473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

No ViewState for DropdownList in DataGrid in UserControl

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
0 1756

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josema | last post by:
Hi to all, I have a webform.aspx and inside i have a dropdown list with autopostback and a place holder... Depending of the selected index that the user selects in the dropdownlist i load a...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
2
by: Steve Pierce | last post by:
I am having some issues with a runtime dropdownlist in a datagrid. The issue is that I cannot get ViewState to fill the selected index of a runtime dropdown properly on postback. I do not want to...
11
by: Nad | last post by:
Hello, I have a page with two link buttons. When I click on the first one I call server.execute("target.aspx") to view target.aspx on the original page. Then I click on the second link button...
5
by: Richard Myers | last post by:
Hello I have a datagrid that has a TemplatedColumn containing a dropdown list (DDL). This DDL is filled for each datagrid row via the itemcreated created event. The first time i postback none...
12
by: TB | last post by:
Hi All: I am trying to create a variation on the standard datagrid, whereby the datagrid is only shown after pressing some buttons. This reason for this is that I would like to use the same...
1
by: Tim | last post by:
Greetings! I have a UserControl. On this UserControl is a Panel and a RadioButtonList. The Panel's Visible property is set to false by default. When the user selects a particular...
5
by: John Kotuby | last post by:
Hi all, After more than a year programming with ASP.NET 2.0 and VB I am still finding it difficult to leave some habits from classic ASP behind. this is particularly true with cross-page posting....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.