DataGridItem.FindControl() (REPOST)

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?
 
E

Eliyahu Goldin

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
 
V

vMike

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.
 

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

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top