DataGridItem.FindControl() (REPOST)

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

I have this code:

<asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="SM">
<ItemTemplate>
<asp:Label id="Label2" runat="server" Text='<%#
Container.DataItem("SM") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlSM" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlSM_SelectedIndexChanged">
<asp:ListItem>... select</asp:ListItem>
<asp:ListItem Value="1000">1000 - AAA</asp:ListItem>
<asp:ListItem Value="2000">2000 - BBB</asp:ListItem>
<asp:ListItem Value="3000">3000 - CCC</asp:ListItem>
<asp:ListItem Value="4000">4000 - DDD</asp:ListItem>
<asp:ListItem Value="5000">5000 - EEE</asp:ListItem>
<asp:ListItem Value="6000">6000 - FFF</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

But, when I execute this code inside Page_Load() :

Dim ddlSM As DropDownList = dg1.FindControl("ddlSM")

it returns Nothing (null). Why?
 
Because your ddl belongs to the frid item rather that to the grid itself.
You can find it with a FindControl call for an item.

Eliyahu
 
John Smith said:
But, when I execute this code inside Page_Load() :

Dim ddlSM As DropDownList = dg1.FindControl("ddlSM")

it returns Nothing (null). Why?


You have to "drill" down a bit deeper to find it.

you might try something like dg1.item.findcontrol("ddlSM") or maybe even
dg1.item.cells.findcontrol... it is tricky as it depends on if you are in
edit mode or not etc. also you will probably have to directcast or ctype it
to a dropdown box. as in
dim txt as textbox = ctype(e.item.findcontrol("QtyChg"),textbox) in this
case e is DataGridItemEventArgs because I am in edit mode.
 
Back
Top