In a site I'm currently writing, I have a page in which users can add entries to the database. The entries are very simple, consisting of selections from three drop down lists.
In order to make it easier on the user, I would prefer to allow multiple entries to be created on the same page. To keep the page clutter down, I want the user to be able to click on a button to add another row for entry creation on the same page.
For a visual example of what I mean, check out this link. At the very bottom under "advanced search using boolean charts" is exactly what I want to do. Upon clicking "add another boolean chart" a new row is added to the page.
How can I implement this in asp.net/c#?
Thank you and anwers will be given credit,
You can use an ASP.NET Table control to do this. There might be other options but this is the simplest I can think of. Just create an asp.net table in the design view, create a header row if you want. Then in the code behind for the button click event, just create new controls (textbox) and add them to cells in rows.
Here is an article that will help you go forward with this idea.
Let Me Google That For You!
In my code, I have a function that will add a new row to the table. When the button gets clicked the first time, it adds the second row successfully. On the second button click, though, the page overwrites the second row and inserts a new row into that position. I have a feeling that the problem is that the "add" button refreshes the page, and then calls the add row function.
The values selected for the first row are always maintained across postback, while the values in the second row are lost the second time the add button is pressed. This is why I think that it's a problem with the page reloading.
Hmmm...I can see the problem you are facing...what I can think of though is that you have to keep a track of row numbers in a session variable. Check the count before adding a row and add rows before hand depending on the number of rows already added. What this means is that you have to add older rows everytime the button is clicked depending on the row count and I would think you also have to keep a track of controls and their values.
Do I make any sense?
Let Me Google That For You!
You definitely make sense, but I don't know a lot about session variables and have no idea how to actually implement this. If you have time, would you mind providing some example code? It would probably help me greatly.
public partial class Administration_AddDependencies : System.Web.UI.Page
private int totalRows = 0;
private int currentRows = 0;
protected void Page_Load(object sender, EventArgs e)
protected void AddDependencyRow()
DebugLabel.Text = "Current rows = " + currentRows.ToString();
DebugLabel.Text += "<br/>Total rows = " + totalRows.ToString();
TableRow tr = new TableRow();
// Create column 1
TableCell td1 = new TableCell();
DropDownList leftDropDownList = new DropDownList();
leftDropDownList.DataSourceID = "OptionsDataSource";
leftDropDownList.ID = "leftDropDownList" + totalRows.ToString();
// Create column 2
TableCell td2 = new TableCell();
DropDownList centerDropDownList = new DropDownList();
centerDropDownList.ID = "centerDropDownList" + totalRows.ToString();
// Create the dropdownlist controls and add them to its items
ListItem item1 =
ListItem item2 =
ListItem item3 =
ListItem item4 =
ListItem item5 =
// Create column 3
TableCell td3 = new TableCell();
DropDownList rightDropDownList = new DropDownList();
rightDropDownList.DataSourceID = "OptionsDataSource";
rightDropDownList.ID = "rightDropDownList" + totalRows.ToString();
// Add the columns to the row
// Add the row to the table
leftDropDownList.DataTextField = "Name";
leftDropDownList.DataValueField = "OptionID";
rightDropDownList.DataTextField = "Name";
rightDropDownList.DataValueField = "OptionID";
private void InitializeGrid()
currentRows = 0;
protected void SeriesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
protected void AddRowButton_Click(object sender, EventArgs e)
Does anybody have any ideas?