DataGrid ItemTemplate naming questions

J

Jim Bancroft

Hi,

I have a DataGrid that contains an ItemTemplate. Within the
ItemTemplate I have a DropDownList. The relevant code looks like this:

<asp:datagrid id="DataGrid1" runat="server">
................
<Columns>
<asp:TemplateColumn HeaderText="Division">
<ItemTemplate>
<asp:DropDownList runat="server" id="Dropdownlist1"
DataTextField="Name" DataValueField="DepartmentID" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
..............................

When the page runs my DropDownList morphs into an html <select> tag, as I'd
expect. However the ID of the tag is a little strange: what happens is the
DataGrid prepends some information to it, so that instead of <select
ID='Dropdownlist1'..." I get "<select
ID='DataGrid1:_ctl2:Dropdownlist1'...."

I understand why the DataGrid does this --not good having a batch <select>
tags on the page with the same ID-- but I'm worried about how consistent the
names will be. For instance, if I add additional ItemTemplates (with other
controls) to this DataGrid will my existing DropDownList IDs change? I read
these <select> values on postback, using the Request object, and if their
IDs change unexpectedly I'll be hurting....I just want to see what my risks
are. Thanks for your advice.

-Jim
 
J

Jim Bancroft

Hey, thanks for those links Scott-- very handy.

I think what I'm trying to do is discussed at the bottom of your second
article: I have a submit button at the bottom of my page, unattached to my
DataGrid. After clicking and submitting the page I need to loop through the
DataGrid's rows and extract the DropDown values. Can I do something like
this?

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in DataGrid1.Items)
{
DropDownList ddl = (DropDownList) item.FindControl("Dropdownlist1");
//do something here
}
}

Does this sound like an okay plan, or is there a better way?
 
S

Scott Allen

That looks like a good approach, Jim. With this approach there is no
need to worry about what the ClientID happens to be, the code just
needs the Control ID which you have complete control over.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top