By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

No ViewState for DropdownList in DataGrid in UserControl

P: n/a
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!
Oct 10 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.