Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Using FindControl on editable datagrid

Reply
Thread Tools

Using FindControl on editable datagrid

 
 
=?Utf-8?B?emFtYml6emk=?=
Guest
Posts: n/a
 
      06-03-2005
....I can't seem to get my hands on a control I'm loading in an editable
datagrid.

Here's my datagrid control:

<asp:datagrid
id="GLRulesGrid"
runat="server"
autogeneratecolumns="False"
oneditcommand="GLRulesGrid_Edit"
ondeletecommand="GLRulesGrid_Delete"
onupdatecommand="GLRulesGrid_Update"
oncancelcommand="GLRulesGrid_Cancel"
cssclass="embedded">
<headerstyle cssclass="large-title-red"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
updatetext="Save" canceltext="Cancel" />
<asp:templatecolumn headertext="Transaction Type">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist
id="TransTypeList"
onload="TransTypeList_Load"
datasource="<%# TransTypes %>"
runat="server" />
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Description">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox
id="DescBox"
runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

In the dropdown list in the first column...I'm simply loading this
collection in the code behind class:

protected IList TransTypes
{
get
{
IList tTypes = new ArrayList();
tTypes.Add("RECV");
tTypes.Add("ADJUST");
tTypes.Add("JENSEN");
tTypes.Add("VAP");
tTypes.Add("SAMPLE");
tTypes.Add("SENT");

return tTypes;
}
}

....which works just fine...but I want to be able to select the current value
in the datagrid cell in the DropDownList control so the user doesn't
accidentally update the record w/ the wrong value, which would be the default
selected item in the DDL.

You'll see that I tried to use the Load event for the DDL but I'm still
unable to use the FindControl method in the code behind to get ahold of the
dropdownlist.

How can I achieve this?

Thanks!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SW50ZWxZb2dp?=
Guest
Posts: n/a
 
      06-03-2005
zambizzi,

You to find the Dropdown control in Itemdatabound event of the grid and
set the selected value of the dropdrop from the one found in your dataitem

Example in your Itemboundevent add this condition

if (itemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView) e.Item.DataItem;

if (objDataRowView["TransactionType"] != null && objDataRowView
["TransactionType"] !=System.DBNull.Value)
{
DropDownList cboTransType =
(DropDownList)e.Item.Cells[2].FindControl
("TransTypeList");
cboTransType.Items.FindByText((string)objDataRowVi ew
["TransactionType"]).Selected = true;
cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
}

}

In this way the dropdownlist will show the values selected which was there
in the itemtemplate.

Note :No need for onload="TransTypeList_Load"

Hope this thing works for you

Regards

IntelYogi


"zambizzi" wrote:

> ...I can't seem to get my hands on a control I'm loading in an editable
> datagrid.
>
> Here's my datagrid control:
>
> <asp:datagrid
> id="GLRulesGrid"
> runat="server"
> autogeneratecolumns="False"
> oneditcommand="GLRulesGrid_Edit"
> ondeletecommand="GLRulesGrid_Delete"
> onupdatecommand="GLRulesGrid_Update"
> oncancelcommand="GLRulesGrid_Cancel"
> cssclass="embedded">
> <headerstyle cssclass="large-title-red"></headerstyle>
> <columns>
> <asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
> updatetext="Save" canceltext="Cancel" />
> <asp:templatecolumn headertext="Transaction Type">
> <itemtemplate>
> <%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
> </itemtemplate>
> <edititemtemplate>
> <asp:dropdownlist
> id="TransTypeList"
> onload="TransTypeList_Load"
> datasource="<%# TransTypes %>"
> runat="server" />
> </edititemtemplate>
> </asp:templatecolumn>
> <asp:templatecolumn headertext="Description">
> <itemtemplate>
> <%# DataBinder.Eval(Container.DataItem, "Description") %>
> </itemtemplate>
> <edititemtemplate>
> <asp:textbox
> id="DescBox"
> runat="server"
> text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
> </edititemtemplate>
> </asp:templatecolumn>
> </columns>
> </asp:datagrid>
>
> In the dropdown list in the first column...I'm simply loading this
> collection in the code behind class:
>
> protected IList TransTypes
> {
> get
> {
> IList tTypes = new ArrayList();
> tTypes.Add("RECV");
> tTypes.Add("ADJUST");
> tTypes.Add("JENSEN");
> tTypes.Add("VAP");
> tTypes.Add("SAMPLE");
> tTypes.Add("SENT");
>
> return tTypes;
> }
> }
>
> ...which works just fine...but I want to be able to select the current value
> in the datagrid cell in the DropDownList control so the user doesn't
> accidentally update the record w/ the wrong value, which would be the default
> selected item in the DDL.
>
> You'll see that I tried to use the Load event for the DDL but I'm still
> unable to use the FindControl method in the code behind to get ahold of the
> dropdownlist.
>
> How can I achieve this?
>
> Thanks!

 
Reply With Quote
 
 
 
 
=?Utf-8?B?emFtYml6emk=?=
Guest
Posts: n/a
 
      06-03-2005
OK, with a little poking and prodding of your example I got it to *almost*
work. I can now get a grip on the drop down list but am unable to select the
item w/ the current dataitem value...here's my event handler for the datagrid
OnItemDataBound event.

protected void GLRulesGrid_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;

if (itemType == ListItemType.EditItem)
{
//set row editable
if (e.Item.DataItem != null)
{
IGLRule rowRule = (IGLRule)e.Item.DataItem;

DropDownList transTypes =
(DropDownList)e.Item.Cells[1].FindControl("TransTypeList");
transTypes.Items.FindByValue(rowRule.TransactionTy pe).Selected = true;
}
}
}

....I'm using an object model to represent data....that's what the type
"IGLRule" is...FYI. I walked through it in the debugger and the data is
working fine...I can read all of my values....including the "TransactionType"
property of the rowRule variable (which is a business object with a type of
IGLRule.) it fails on the line where I try to the ddl Selected to
true....the last line.

I get this error:

"Object reference not set to an instance of an object."

....which doens't make sense....I can read from my business object's
properties AND from the dropdownlist declared one line before the one that
fails....why can't I select it?

Thanks!

"IntelYogi" wrote:

> zambizzi,
>
> You to find the Dropdown control in Itemdatabound event of the grid and
> set the selected value of the dropdrop from the one found in your dataitem
>
> Example in your Itemboundevent add this condition
>
> if (itemType == ListItemType.EditItem)
> {
> DataRowView objDataRowView = (DataRowView) e.Item.DataItem;
>
> if (objDataRowView["TransactionType"] != null && objDataRowView
> ["TransactionType"] !=System.DBNull.Value)
> {
> DropDownList cboTransType =
> (DropDownList)e.Item.Cells[2].FindControl
> ("TransTypeList");
> cboTransType.Items.FindByText((string)objDataRowVi ew
> ["TransactionType"]).Selected = true;
> cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
> }
>
> }
>
> In this way the dropdownlist will show the values selected which was there
> in the itemtemplate.
>
> Note :No need for onload="TransTypeList_Load"
>
> Hope this thing works for you
>
> Regards
>
> IntelYogi
>
>
> "zambizzi" wrote:
>
> > ...I can't seem to get my hands on a control I'm loading in an editable
> > datagrid.
> >
> > Here's my datagrid control:
> >
> > <asp:datagrid
> > id="GLRulesGrid"
> > runat="server"
> > autogeneratecolumns="False"
> > oneditcommand="GLRulesGrid_Edit"
> > ondeletecommand="GLRulesGrid_Delete"
> > onupdatecommand="GLRulesGrid_Update"
> > oncancelcommand="GLRulesGrid_Cancel"
> > cssclass="embedded">
> > <headerstyle cssclass="large-title-red"></headerstyle>
> > <columns>
> > <asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
> > updatetext="Save" canceltext="Cancel" />
> > <asp:templatecolumn headertext="Transaction Type">
> > <itemtemplate>
> > <%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
> > </itemtemplate>
> > <edititemtemplate>
> > <asp:dropdownlist
> > id="TransTypeList"
> > onload="TransTypeList_Load"
> > datasource="<%# TransTypes %>"
> > runat="server" />
> > </edititemtemplate>
> > </asp:templatecolumn>
> > <asp:templatecolumn headertext="Description">
> > <itemtemplate>
> > <%# DataBinder.Eval(Container.DataItem, "Description") %>
> > </itemtemplate>
> > <edititemtemplate>
> > <asp:textbox
> > id="DescBox"
> > runat="server"
> > text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
> > </edititemtemplate>
> > </asp:templatecolumn>
> > </columns>
> > </asp:datagrid>
> >
> > In the dropdown list in the first column...I'm simply loading this
> > collection in the code behind class:
> >
> > protected IList TransTypes
> > {
> > get
> > {
> > IList tTypes = new ArrayList();
> > tTypes.Add("RECV");
> > tTypes.Add("ADJUST");
> > tTypes.Add("JENSEN");
> > tTypes.Add("VAP");
> > tTypes.Add("SAMPLE");
> > tTypes.Add("SENT");
> >
> > return tTypes;
> > }
> > }
> >
> > ...which works just fine...but I want to be able to select the current value
> > in the datagrid cell in the DropDownList control so the user doesn't
> > accidentally update the record w/ the wrong value, which would be the default
> > selected item in the DDL.
> >
> > You'll see that I tried to use the Load event for the DDL but I'm still
> > unable to use the FindControl method in the code behind to get ahold of the
> > dropdownlist.
> >
> > How can I achieve this?
> >
> > Thanks!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid EditItemCommand : must click twice to make DataGrid Editable Diane ASP .Net Datagrid Control 5 11-09-2005 09:54 AM
findcontrol in datagrid footer Ruby ASP .Net 0 01-28-2004 05:11 AM
DataGrid FindControl HELP... Mike Speak ASP .Net 1 11-19-2003 08:04 PM
Datagrid FindControl in Footer Dave ASP .Net 2 11-14-2003 04:11 PM
Use FindControl for HMTL control in Datagrid Sid ASP .Net 4 11-11-2003 09:24 AM



Advertisments