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

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

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.