Cannot find Control DropDownList in datagrid

J

Jeff User

Hi
C# .NET1.1

I have read Visual Studio help and several articles on the web.
Obviously this is a common topic. However, I see this code over and
over yet I am stuck with the FindControl method when trying to locate
my DropDownList so that I can populate it. It doesn't find the control

Here is what I have:
asp.net:
<asp:datagrid id="dgTable"
style= REMOVED FOR BREVITY...
runat="server"
OnItemCommand="dgTable_Command"
OnUpdateCommand="dgTable_Update"
OnCancelCommand="dgTable_Cancel"
OnEditCommand="dgTable_Edit"

BorderStyle="Outset" BackColor="#E0E0E4"
AutoGenerateColumns="False"

OnItemDataBound="dgTable_ItemDataBound">

<EditItemStyle blah blah blah </ItemStyle>

<Columns>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" HeaderText="Edit item"
CancelText="Cancel" EditText="Edit">
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="Name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>Type</HeaderTemplate>
<ItemTemplate>
<!--- The next line binds to my dataSet correctly when not in edit
mode ---->>>
<asp:Label id=Label3 runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Type")
%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<!-- Next id is never found in C# code behind --->
<asp:DropDownList id="ddlType"
runat="server" Width="150">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn
DataField="PK" HeaderText="PK">
</asp:BoundColumn>
<<<AND then a few more basic BoundColumns like the above >>>>
Then in C# :
protected void dgTable_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem )
{
string[] options = { "Option1", "Option2", "Option3" };
// The NEXT Line Fails to find my control
DropDownList list =
(DropDownList)e.Item.FindControl("ddlType");
//Thus, I needed this If statement to keep from crashing
//because "list" is always null
if (list !=null)
{
list.DataSource = options;
list.DataBind();
}
}
}

I tried to replace
(DropDownList)e.Item.FindControl("ddlType");
with
(DropDownList)dgTable.Items.FindControl("ddlType");
Which also never finds the control.

What am I doing wrong here to find the control?

Thanks in advance !!
Jeff
 
B

Brock Allen

The EditItemTemplate is only populated when that row is in EditMode. Check
for ListItemType.EditItem to find the row that's been populated with your
EditItemTemplate.
 
J

Jeff User

Brock

Excellent !!
So simple when done correctly
:eek:)
Thanks
jeff

The EditItemTemplate is only populated when that row is in EditMode. Check
for ListItemType.EditItem to find the row that's been populated with your
EditItemTemplate.


Hi
C# .NET1.1
I have read Visual Studio help and several articles on the web.
Obviously this is a common topic. However, I see this code over and
over yet I am stuck with the FindControl method when trying to locate
my DropDownList so that I can populate it. It doesn't find the control

Here is what I have:
asp.net:
<asp:datagrid id="dgTable"
style= REMOVED FOR BREVITY...
runat="server"
OnItemCommand="dgTable_Command"
OnUpdateCommand="dgTable_Update"
OnCancelCommand="dgTable_Cancel"
OnEditCommand="dgTable_Edit"
BorderStyle="Outset" BackColor="#E0E0E4"
AutoGenerateColumns="False"
OnItemDataBound="dgTable_ItemDataBound">

<EditItemStyle blah blah blah </ItemStyle>

<Columns>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" HeaderText="Edit item"
CancelText="Cancel" EditText="Edit">
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="Name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>Type</HeaderTemplate>
<ItemTemplate>
<!--- The next line binds to my dataSet correctly when not in edit
mode ---->>>
<asp:Label id=Label3 runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Type")
%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<!-- Next id is never found in C# code behind --->
<asp:DropDownList id="ddlType"
runat="server" Width="150">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn
DataField="PK" HeaderText="PK">
</asp:BoundColumn>
<<<AND then a few more basic BoundColumns like the above >>>>
Then in C# :
protected void dgTable_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem )
{
string[] options = { "Option1", "Option2", "Option3" };
// The NEXT Line Fails to find my control
DropDownList list =
(DropDownList)e.Item.FindControl("ddlType");
//Thus, I needed this If statement to keep from crashing
//because "list" is always null
if (list !=null)
{
list.DataSource = options;
list.DataBind();
}
}
}
I tried to replace
(DropDownList)e.Item.FindControl("ddlType");
with
(DropDownList)dgTable.Items.FindControl("ddlType");
Which also never finds the control.
What am I doing wrong here to find the control?

Thanks in advance !!
Jeff
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top