473,387 Members | 1,899 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,387 software developers and data experts.

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">
<HeaderTemplate>
<th>Category</th>
<th>SubCategory</th>
<th>Field</th>
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="Category" Runat="server"
DataSource="<%#CategoryDDL%>" DataTextField="category"
DataValueField="categoryID"></asp:DropDownList>
<asp:DropDownList ID="SubCategory" Runat="server"
DataSource="<%#SubCatDDL%>" DataTextField="subcat"
DataValueField="subcategoryID"></asp:DropDownList>
<asp:DropDownList ID="Field" Runat="server"
DataSource="<%#FieldDDL%>" DataTextField="field"
DataValueField="fieldID"></asp:DropDownList>
</ItemTemplate>
</asp:datalist>
<asp:Button id="AddRow" Text="Add Condition"
Runat="server"></asp:Button>
(I have simplified the code somewhat though, removing
AutoPostBack=True, EnableViewState=True, and event handlers from the
DropDownList entries)
CODE BEHIND:

/// <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
{
get
{
if( ViewState["gridRows"] != null )
return (int) ViewState["gridRows"];
else
return 0;
}
set
{
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 )
{
PopulateDropDowns();

gridRows = 6; // Default Query Builder row size

QueryGrid.DataSource = GetTable();
QueryGrid.DataBind();
}

}

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)
{
gridRows++;

//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 1680

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

Similar topics

1
by: Mike Malter | last post by:
I take it that the only way to add rows to a DataList control is through DataBind? If not, how do I manually add rows to a DataList control? I have searched everywhere and can't find an example...
0
by: pei_world | last post by:
I am using the following code to configurate my datagrid and load data from my Pocket Access Database. however, I got some more row extra, and it is the number I setted with my object array in my...
0
by: Chris Ericoli | last post by:
Hi, I am wanting to add a usercontrol programatically to a placeholder's control collection. The placeholder is coded within the ITEMTEMPLATE of a datalist. The problem that I am facing, is...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Scott Schluer | last post by:
Hello, I've got myself a small problem and I'm hoping someone can help. I have a DataList called dlProducts (displays products from a database). Within the <ItemTemplate> container of the...
1
by: Mihai_Panait | last post by:
I'm trying to build a commerce site and i can't understand why my DataList controls acts diferent with the same code in the same page My database contains amoung others 'category', 'subcategory' and...
3
by: anon | last post by:
Hi All, I would like to make a datalist which takes new information by allowing users(with some button or link) to add new rows to a datalist with some information and save the information.....is...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
0
by: Isz | last post by:
PROBLEM: This problem is related to the postback and handling events correctly. BACKGROUND: I have a datalist which acts as a tabbes list with horizontal layout. This datalist is bound to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.