470,822 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,822 developers. It's quick & easy.

How do I add rows to a DataList without losing state of controls?

I have a DataList on my page which contains multiple DropDownLists. My
page defaults to showing six rows in the DataList. I want the ability
to dynamically add rows if needed. Right now I'm just using an empty
table with X rows, and adding a new row to the table that the DataList
is bound to. However, I can't figure out a way to do this and at the
same time maintain the current viewstate of the existing controls.
When I
call DataList.BindData() to re-bind the list, the state of the
drop-downs and their contents are lost.

HOW do I add a row to the DataList programmatically AND keep the
viewstate? (see the AddRow_Click event below)

It seems odd to have to bind the DataList to an empty table in order to
get my ASPX code to generate rows for the DataList. Is there a better
way to do this?
ASPX code:

<asp:datalist id="QueryGrid" Runat="server" EnableViewState="True">
<asp:DropDownList ID="Category" Runat="server"
DataSource="<%#CategoryDDL%>" DataTextField="category"
<asp:DropDownList ID="SubCategory" Runat="server"
DataSource="<%#SubCatDDL%>" DataTextField="subcat"
<asp:DropDownList ID="Field" Runat="server"
DataSource="<%#FieldDDL%>" DataTextField="field"
<asp:Button id="AddRow" Text="Add Condition"
(I have simplified the code somewhat though, removing
AutoPostBack=True, EnableViewState=True, and event handlers from the
DropDownList entries)

/// <summary>
/// Summary description for QueryBuild.
/// </summary>
public class QueryBuild : System.Web.UI.Page, INamingContainer
protected DataTable CategoryDDL;
protected DataTable SubCatDDL;
protected DataTable FieldDDL;
protected DataTable ValueDDL;

# region private int gridRows
// Tracks the number of rows we display in the Query Builder
private int gridRows
if( ViewState["gridRows"] != null )
return (int) ViewState["gridRows"];
return 0;
ViewState["gridRows"] = value;

} // end gridRows
# endregion
private void Page_Load(object sender, System.EventArgs e)
// Put user code to initialize the page here

if( !IsPostBack )

gridRows = 6; // Default Query Builder row size

QueryGrid.DataSource = GetTable();


private DataTable GetTable()
DataTable grid = new DataTable();

// THIS IS AN EMPTY TABLE -- is there a better way to do this?
for(int rowCount = 0; rowCount < gridRows; rowCount++)
grid.Rows.Add( grid.NewRow() );

return grid;

private void PopulateDropDowns()
DataTable paramTable = GetParameterTable();

CategoryDDL = GetCategories(paramTable);
SubCatDDL = GetSubCategories(paramTable, "EMPTY");
FieldDDL = GetFields(paramTable, "EMPTY");

private void AddRow_Click(object sender, System.EventArgs e)

//HELP!? How do I programmatically add a row to my DataList without
losing the state of
// the existing drop-downs?

QueryGrid.DataSource = GetTable();
QueryGrid.DataBind(); // this CLEARS my existing state! But if I
don't re-bind, then the
// row will not be added to the DataList


Nov 19 '05 #1
0 1586

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike Malter | last post: by
reply views Thread by pei_world | last post: by
reply views Thread by Chris Ericoli | last post: by
3 posts views Thread by Jim Heavey | last post: by
1 post views Thread by Scott Schluer | last post: by
1 post views Thread by Mihai_Panait | last post: by
3 posts views Thread by anon | last post: by
2 posts views Thread by Shiju Poyilil | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.