No ViewState for DropdownList in DataGrid in UserControl

S

stevem2112

I have a datagrid with 2 Template columns. One column has
DropDownLists and the other has Textboxes. I bind each DDL in the
ItemCreated event. This datagrid is inside a UserControl that is
inside a user control (long story). Anyway, the user clicks a save
button on the parent control to initiate the postback. The parent then
calls a Save method (in the child control) from the button event
handler. When I iterate through the datagrid grabbing DDLs and TBs,
the TBs maintain viewstate and I can grab values entered by the user.
However, the DDLs always reflect the first item in the list. No
viewstate at all. I have tried various ways of binding everything but
nothing has worked so far. Any suggestions would be helpful....


<asp:DataGrid ID="dgGroups" runat="server" AllowSorting="false"
AllowPaging="false"
ShowHeader="False" AutoGenerateColumns="False"
CellPadding="0" Width="100%" BorderStyle="None"
EnableViewState="True" BorderWidth="0px"
OnItemDataBound="dgGroups_ItemDataBound"
OnItemCreated="dgGroups_ItemCreated">
<AlternatingItemStyle CssClass="ProfileFieldLabel"></
AlternatingItemStyle>
<ItemStyle CssClass="ProfileFieldLabel"></ItemStyle>
<HeaderStyle CssClass="MenuHeader"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Group_ID"
HeaderText="GroupID" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="Value"
HeaderText="Value" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="Name"
HeaderText="Groups" Visible="false"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Groups">
<ItemStyle Width="173px"
HorizontalAlign="Right"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"Name") + ":&nbsp;" %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions"
runat="server" CssClass="csdropdown" Width="100px"
EnableViewState="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<asp:TextBox ID="tbOption" runat="server"
CssClass="cstextbox" Width="100px" EnableViewState="True"></
asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


protected void dgGroups_ItemCreated(object sender,
DataGridItemEventArgs e)
{
DataGridItem dgi;
//CustomCSS css;

// Get row reference
dgi = e.Item;

if (dgi.ItemType == ListItemType.Item || dgi.ItemType ==
ListItemType.AlternatingItem)
{
//Grab Controls
DropDownList ddl = null;
TextBox tb = null;

ddl = (DropDownList)dgi.FindControl("ddlOptions");
tb = (TextBox)dgi.FindControl("tbOption");

ddl.DataSource =
GetOptions(Convert.ToInt32(DataBinder.Eval(e.Item.DataItem,
"Group_ID")));
ddl.DataTextField = "Value";
ddl.DataValueField = "Group_ID";
ddl.DataBind();
}
}

protected void dgGroups_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
DataGridItem dgi;
//CustomCSS css;

// Get row reference
dgi = e.Item;

if (dgi.ItemType == ListItemType.Item || dgi.ItemType ==
ListItemType.AlternatingItem)
{
if (!IsPostBack)
{
//Grab Controls
DropDownList ddl = null;
TextBox tb = null;

ddl = (DropDownList)dgi.FindControl("ddlOptions");
tb = (TextBox)dgi.FindControl("tbOption");

//Set Value
if
(ddl.Items.FindByText(dgi.Cells[(int)GridColumns.Value].Text) != null)
{
ddl.ClearSelection();

ddl.Items.FindByText(dgi.Cells[(int)GridColumns.Value].Text).Selected
= true;
}
else
{
tb.Text =
dgi.Cells[(int)GridColumns.Value].Text;
}
}
}
}

Thank You!
 

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,707
Messages
2,569,343
Members
44,635
Latest member
Matt231

Latest Threads

Top