Adding a new row of controls on button click

Hi all,
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,
WinterPhoenix

0
WinterPhoenix
9/16/2008 3:41:39 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

7 Replies
1100 Views

Similar Articles

[PageSpeed] 47

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.

http://www.dotnetspider.com/resources/750-Dynamically-creating-ASP-NET-controls-from-e-cod.aspx


Thanks,
Max
Let Me Google That For You!
0
bullpit
9/16/2008 4:24:35 PM

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.

Any ideas?

0
WinterPhoenix
9/16/2008 6:38:23 PM

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?


Thanks,
Max
Let Me Google That For You!
0
bullpit
9/16/2008 7:26:26 PM

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.

Thanks,
WinterPhoenix

0
WinterPhoenix
9/16/2008 7:50:21 PM

Can you post your code?


Thanks,
Max
Let Me Google That For You!
0
bullpit
9/16/2008 7:59:28 PM

 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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)
    {
        InitializeGrid();
    }

    protected void AddDependencyRow()
    {
        currentRows++;
        totalRows++;

        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();
        td1.Controls.Add(leftDropDownList);

        // 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 =
            new ListItem("Item1");
        ListItem item2 =
            new ListItem("Item2");
        ListItem item3 =
            new ListItem("Item3");
        ListItem item4 =
            new ListItem("Item4");
        ListItem item5 =
            new ListItem("Item5");
        centerDropDownList.Items.Add(item1);
        centerDropDownList.Items.Add(item2);
        centerDropDownList.Items.Add(item3);
        centerDropDownList.Items.Add(item4);
        centerDropDownList.Items.Add(item5);

        td2.Controls.Add(centerDropDownList);
       
        // Create column 3
        TableCell td3 = new TableCell();
        DropDownList rightDropDownList = new DropDownList();
        rightDropDownList.DataSourceID = "OptionsDataSource";
        rightDropDownList.ID = "rightDropDownList" + totalRows.ToString();
        td3.Controls.Add(rightDropDownList);

        // Add the columns to the row
        tr.Cells.Add(td1);
        tr.Cells.Add(td2);
        tr.Cells.Add(td3);

        // Add the row to the table
        ControlTable.Rows.Add(tr);

        leftDropDownList.DataTextField = "Name";
        leftDropDownList.DataValueField = "OptionID";
        rightDropDownList.DataTextField = "Name";
        rightDropDownList.DataValueField = "OptionID";
    }

    private void InitializeGrid()
    {
        currentRows = 0;
        AddDependencyRow();

    }
    protected void SeriesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitializeGrid();
    }
    protected void AddRowButton_Click(object sender, EventArgs e)
    {
        AddDependencyRow();
    }
}

0
WinterPhoenix
9/16/2008 8:27:24 PM

Does anybody have any ideas?

0
WinterPhoenix
9/17/2008 2:01:48 PM
Reply:

Similar Artilces:

Adding row (one at a time) in gridview on a button click. And cells of added row should contain controls like DROPDOWN LIST or TEXT BOX.
Dear Friends, I am in great need for the solution/hints/guidance of my requirement, if anyone can help with.  Let me explain what I need."I need a Gridview control on a webpage having following 5 columns/fields.     Column 1: It just has a "+" button in its header only (other cells for this column is empty)    Column 2: The Column header here is "FIELD" and  it should contain dropdown list (which is populated from some database)    Column 3: The Column header here is "Operator" and  it should ...

Urgent!!! Form inside Windows form control (that is embedded to Web page i.e aspx), not displayed on button click
 Hi all I have a Windows Form Control(i.e Windows Control Library), that consists of a Button "button1" and  a panel "panel1". Also I have a form "form1.cs". On form "form1.cs" I have a label whose text value i have set to  "Hi". On click of button "button1" on windows form control i am opening the form "form1.cs"  inside a panel and it displays "Hi". The code of windows form control is as below :  public partial class UserControl1 : UserControl    {    ...

adding row to a table control on button click
 Hi all, I'm trying to do something rather straightforward, but it's not working.I'm creating some sort of timesheet application where I want to be able to add rows with some date fields to my table control after I click on a button "add new row". This is the code I have behind the button "add row", and it works the first time I click it, but when I click it a second or a third time, no rows are being added... Any idea on how to solve this? Screenshot is here:  1 Protected Sub btnAddRow_Click(ByVal sender As Object, ByVal e As System.E...

Adding a new row to a table when a button is clicked
I have a table and when a button is clicked a new row is added to the table. The row is added to the bottom however. Can I make the new row be added to the middle of the table??Thanks To clarify - this table is used simply to input data, the new row that is added is only added on the webpage and not to the database.Any suggestions would be great!!! Ok I just did this:table1.Rows.Insert(1, tr)...

Adding a new row in the gridview on click of add button..
Hi, I have a gridview which gets populated by calling BLL method . My BLL returns Datatable and am binding it to the gridview.Now i want to add new row.For that i have written aspx as below:   <asp:GridView ID="gvSubContractor" runat="server" AutoGenerateColumns="False" EmptyDataText="No Data"Width="959px" ShowFooter="false" OnRowCreated="gvSubContractor_RowCreated" onrowcommand="gvSubContractor_RowCommand"> <RowStyle CssClass="ItemStyle" /> <Columns> <asp:TemplateField...

How to determine the row number when clicking a delete button I added to a template column from a GridView control???
Hello all,I am working using the codebehinde the page with VB.NET 2005 (web application).  I have a gridview where I added a template column, inside that templated column I added a delete button.  I would like to know how could I determine which row number corresponds to the clicked button so I can delete the current record.This is what I have: Protected Sub LinkButtonDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim cmd As SqlCommand Dim objConnection As SqlConnection objConnection = New SqlConnecti...

when ever a user click on add button a new row with another dropdown lists should be added to an existing table
Hi everyone, I am adding a Table(server control)  to a usercontrol . the table consists a row containing drop down lists with add button . when ever a user click on add button a new row with another dropdown lists should be added.is there any way to copy existing row? so that it uses AJAX not page post back .  I tried to a add new row in button click event to the table. even though a row is showing up on the user control as in pre-render  but it is not adding to the table i intended to add when the page is post backed i.e when the page is reloaded it shows a initial ...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

Need HELP with adding a System.Windows.Forms control to web form!
Howdy folks. I'm in need of some help. I wanted to add a nifty little Control I made for a Windows Form to an ASP.NET page. Now I went and saw Microsoft's quickstart tutorial, and followed the example... You know, compile it to .dll... blah, blah, blah, then use the <OBJECT> tag to insert it into the page. Well, I did all that... the control downloads... but I can't see a thing! :'( For example, here's some code ~ using System; using System.Windows.Forms; using System.Drawing; using System.Reflection; [assembly:AssemblyVersion("1.0.0.0")] ...

ASPX Web Form window jumps when link button or button is clicked
Dear, i m senthil, and i m new to asp.net I m using asp.net with vb, my problem is, i m creating a forum, in all web pages while clicking any link button or command button the webpage jumps and the focus is going to some other controls. I want the page not to get jumped... Pls help in this regard. set the MaintainScrollPositionOnPostback property of your page to true. http://msdn.microsoft.com/en-us/library/system.web.ui.page.maintainscrollpositiononpostback.aspxMike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.co...

button click handler on .aspx page for user web control button
Hi guys,  I have a web user control which contains my search function(some textfields and a button named btnSearch).  I create a web part manager and a web part zone to put the web user control in it to allow the user to minimise/show the search function   In my web page(.aspx), I have a nested datagrid and the web part manger and the webpart zone. Is there a way get the event handle for the btnSearch.click in the aspx page? This is to allow me to filter the datagrid based on the search critial given in the search function...

Button click on web form
I have button. I want a do two think of button click. Open page as a pop up form and redirect the main page to other page. Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click        If Len(Request.QueryString("request_send").ToString()) > 0 Then ----open pop up form with this link and then redirect to account.aspx page "https://do.test.appx?ID=" & Request.QueryString("request_sendtoicon") "&acc=" & Request.QueryString("cs_no") "&payon=2"      ...

Dynamiclly added web control in a web control problem?
I have a small problem...I have a web control, that creates dynamicly some other my custom controls and add these to itself. But when there is a postback, how can I get the information of the dynamiclly added control. Here is my example:(I have simplified)  1 DropDownList d; 2 protected void Page_Load(object sender, EventArgs e) 3 { 4 d= new DropDownList(); 5 d.Items.Add("B"); 6 d.Items.Add("L"); 7 d.AutoPostBack = true; 8 d.SelectedIndexChanged += new Event...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

Web resources about - Adding a new row of controls on button click - asp.net.web-forms

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

WA bushfires: Firefighters work through the night to control several blazes
Firefighters in Western Australia are working through the night to control and contain several blazes.

Alaska Marijuana Control Board votes to allow consumption in retail outlets - Alaska Dispatch News
The Alaska Marijuana Control Board voted Friday to allow consumption of marijuana products in retail stores across the state. But social clubs, ...

69 percent of users would bypass security controls to win a big deal
When faced with the chance of clinching a major deal people are willing to throw security controls out of the window. This is according to research ...

MAZEL TOV, HILLARY: Clinton Wins Gun Control Award From Major Anti-Bill of Rights Group
... restrictions on the freedoms protected by the Constitution is not a sign of character, but a clear indication of blind adherence to a gun control ...

Obama to Veto Bill Requiring Background Checks On Syrian Refugees; France Demands Tighter Controls, EU ...
... does not call for ending the refugee program or require religious screenings, as some Republicans have demanded . France Demands Tighter Controls, ...

Brussels Pushes Gun Control In Wake of Paris Attacks
Europeans double down on gun control after terrorists murder defenseless civilians

Control Your HTPC With Scavenged IR Parts
If you’ve built yourself a home theater PC, one of your highest priorities is probably coming up with a convenient control solution. The easiest ...

ICYMI: Even the AP couldn’t validate any of Hillary’s gun control nonsense
... – not 200 as she claimed . Asked to explain the discrepancy, Clinton’s campaign pointed to 2013 statistics from the Centers for Disease Control ...

5 Questions: Punjab’s rightful control over Chandigarh, says Prem Singh Chandumajra
Shiromani Akali Dal (Badal) MP Prem Singh Chandumajra on why he raised the issue of Punjab’s claim over Chandigarh in LS.

Resources last updated: 11/25/2015 4:16:35 AM