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
ropDownList id="ddlType"
runat="server" Width="150">
</asp
ropDownList>
</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
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
runat="server" Width="150">
</asp
</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