I've bound a series of drop-down lists in a repeater control to a data
table. I've late bound them to datatable, and am using the
_ItemDataBound event to populate each control accordingly, and change the
name to reflect the ID field from the database.
This all works peachey, but the dynamic changing-of-name doesn't seem to
actually have done anything when I view the HTML source, so I'm apparently
loosing my unique ID for a start. Then when I try to read the form back, I
can't locate the controls using either the generic name they had originally,
or the new dynamic name.
Obviously I'm doing something dumb here, but I'm tearing my hair out trying
to finish this damn app. Can someone please point me in the right
direction?
Thanks in advance,
Dunc
..ASPX page
<asp:Repeater ID="VenueExtras" Runat="server">
<ItemTemplate>
<tr>
<td><strong><%# DataBinder.Eval(Container.DataItem, "ExtraName")
%></strong></td>
<td colspan="2">
<asp:DropDownList ID="VenueExtraItem" Runat="server">
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
..CS - data bind (note: "venue" refers to an instance of a strongly typed
dataset)
private void VenueExtras_ItemDataBound(object sender, RepeaterItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
DropDownList objDDL =
(DropDownList)e.Item.FindControl("VenueExtraItem") ;
objDDL.ID = "VenueExtra_" + row[0].ToString();
// Check to see if this one is in the list
for (int iLoop = 0; iLoop < venue.VenueExtras.Rows.Count; iLoop++)
{
if (venue.VenueExtras[iLoop].VenueExtrasID ==
System.Convert.ToInt32(row[0]))
{
objDDL.SelectedIndex = 1;
}
}
}
}
..CS - get the values (or doesn't as the case may be)
private void SaveData()
{
for (int iLoop = 0; iLoop < dal.VenueExtrasGet().Rows.Count; iLoop++)
{
// This doesn't work
// DropDownList ddl =
(DropDownList)VenueExtras.FindControl("VenueExtra_ " +
dal.VenueExtrasGet().Rows[iLoop][0].ToString());
// Neither does this seem to
DropDownList ddl =
(DropDownList)VenueExtras.FindControl("VenueExtraI tem");
if (ddl != null)
{
Response.Write("Value: " + ddl.ID + "<BR>");
Response.Write("Value: " + ddl.SelectedValue + "<BR>");
}
}
}