You must Sign In to post a response.
  • Category: ASP.NET

    Adding another row with all the textboxes on add button click

    Hello

    I have the code below for aspx page, when I click "Add" button the data in the text boxes should be sent to database and create another row with same set of textboxes ( with add button) , the borders and every style in the table should not change, it should add as many rows, the page can extend down(I mean the user can scroll down )

    What would be the code on Addbutton click

    <tr><td><asp:DropDownList runat="server" ID="DropDownList1" >
    </asp:DropDownList>
    </td>
    <td><asp:RadioButton runat="server" Text="Yes" ID="RadioButton1" GroupName="G1" /><asp:RadioButton runat="server" Text="No" ID="RadioButton2" GroupName="G1" /></td>
    <td><asp:DropDownList runat="server" ID="DropDownList2" /></td>
    <td><asp:TextBox runat="server" ID="TextBox1" /></td>
    <td><asp:TextBox runat="server" ID="TextBox2" /></td>
    <td><asp:TextBox runat="server" ID="TextBox3" /></td>
    <td><asp:TextBox runat="server" ID="TextBox4" /></td>
    <td><asp:TextBox runat="server" ID="TextBox5" /></td>
    <td><asp:TextBox runat="server" ID="TextBox6" /></td>
    <td><asp:TextBox runat="server" ID="TextBox7" /></td>
    <td><asp:TextBox runat="server" ID="TextBox8" /></td>
    <td><asp:Button runat="server" Text="Add" /></td>
    </tr>
  • #765755
    Hi Lexi,

    Just confirm onething whether it is inside gridview or outside gridview?

    If it is outside gridview then on button click event just pass the control values and save it to database.

    Ex:

    Protected void btnSave_Click(object sender, EventArgs e)
    {
    string val1=textbox1.Text;
    string val2=textbox2.Text;
    /// pass those string values as a parameter while insert them into database.
    }


    If you are looking the same inside gridview control then onrowcommand event of gridview, insert the records into database.


    Protected void gv_OnRowCommand(object sender, GridViewRowCommandEventArgs e)
    {
    if(e.CommandName=="Add")
    {
    //find your controls here and insert it into database.
    }
    }


    If you still having any questions please let me know...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765766
    Use gridview with child control, gridview has template control I which you can add new row with controls, see below snippet

    < asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
    < Columns>
    < asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
    < asp:TemplateField HeaderText="Header 1">
    < ItemTemplate>
    < asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    < /ItemTemplate>
    < /asp:TemplateField>
    < asp:TemplateField HeaderText="Header 2">
    < ItemTemplate>
    < asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    < /ItemTemplate>
    < /asp:TemplateField>
    < asp:TemplateField HeaderText="Header 3">
    < ItemTemplate>
    < asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    < /ItemTemplate>
    < FooterStyle HorizontalAlign="Right" />
    < FooterTemplate>
    < asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
    < /FooterTemplate>
    < /asp:TemplateField>
    < /Columns>
    < /asp:gridview>

    //aspx.cs
    private void SetInitialRow()
    {
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dt.Rows.Add(dr);
    //dr = dt.NewRow();
    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dt;
    Gridview1.DataSource = dt;
    Gridview1.DataBind();
    }


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments