Using FindControl on editable datagrid

G

Guest

....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!
 
G

Guest

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)objDataRowView
["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
 
G

Guest

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.TransactionType).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 said:
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)objDataRowView
["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 said:
...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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top