Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please check below code once:

C#
protected void Page_Load(object sender, EventArgs e)
{
Table table = new Table();
table.ID = "tbl_dy";
 
Page.Form.Controls.Add(table);
if (!IsPostBack)
{
for (int i = 1; i < 3; i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
Label lbl = new Label();
lbl.ID = "lbl_'" + i + "'";
if (i == 1)
{
lbl.Text = "EName";
}
else { lbl.Text = "Salary"; }
cell.Controls.Add(new LiteralControl(" "));
TextBox txt = new TextBox();
txt.ID = "txt_'" + i + "'";
cell.Controls.Add(new LiteralControl("
"));
cell.Controls.Add(lbl);
cell.Controls.Add(txt);
row.Cells.Add(cell);
table.Rows.Add(row);
}
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
Label lbl1 = new Label();
lbl1.ID = "lbl_gender";
lbl1.Text = "Gender";
DropDownList ddl = new DropDownList();
ddl.Width = 100;
ddl.Height = 25;
ddl.ID = "ddl_gender";
ddl.Items.Add("Select");
ddl.Items.Add("Male");
ddl.Items.Add("Female");
cell1.Controls.Add(lbl1);
Button btn = new Button();
btn.Width = 60;
btn.Height = 40;
btn.ID = "btn_save";
btn.Text = "Save";
cell1.Controls.Add(new LiteralControl("  "));
cell1.Controls.Add(ddl);
cell1.Controls.Add(new LiteralControl("
"));
cell1.Controls.Add(btn);
row1.Cells.Add(cell1);
table.Rows.Add(row1);
}
}


Now i want to save the data in my database.
Posted
Updated 28-Mar-12 23:10pm
v2
Comments
Herman<T>.Instance 29-Mar-12 6:09am    
please be more specific
ZurdoDev 29-Mar-12 8:37am    
Do you have a DAL? Some class to write to the db? If not, you need to write the code to do this. Either way, you need to write code but we don't know what you have.
Hari Krishna Prasad Inakoti 29-Mar-12 12:28pm    
i didn't use dal dude.

1 solution

HI , 
check this one, by this solution you can create as many as 
you want of text box and u can retrieve the  data from it . and it also can be use this code on various Controls

just let me know your feedback .



ASP.NET
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    &nbsp;<asp:Button ID="Button2" runat="server" Text="show data"
        onclick="Button2_Click" />

      <div id="DivContent" runat="server">
       </div>
    </form>
</body>

Code behind :
C#
protected System.Web.UI.WebControls.TextBox txtSkill;
    protected System.Web.UI.WebControls.TextBox txtVersion;
    protected void Page_Load(object sender, EventArgs e)
    { 
    }     
    int countTimes = 0;
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["countTimes"] == null)
        {
            countTimes = 1;
        }
        else
        {
            countTimes = Convert.ToInt32(ViewState["countTimes"]);
        }

        for (int i = 0; i < countTimes; i++)
        {
            txtSkill = new TextBox();
            txtSkill.ID = "txtSkill" + i;


            txtVersion = new TextBox();
            txtVersion.ID = "txtVersion" + i;

            form1.Controls.Add(txtSkill);
            form1.Controls.Add(txtVersion);

        }
        countTimes = countTimes + 1;

        ViewState.Add("countTimes", countTimes);   

    } 
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ViewState["countTimes"] == null)
        {
            countTimes = 1;
        }
        else
        {
            countTimes = Convert.ToInt32(ViewState["countTimes"]);
        }

        for (int i = 0; i < countTimes -1; i++)
        {
            txtSkill = new TextBox();
            txtSkill.ID = "txtSkill" + i;


            txtVersion = new TextBox();
            txtVersion.ID = "txtVersion" + i;

            form1.Controls.Add(txtSkill);
            form1.Controls.Add(txtVersion);

        }


        string storeToDbValueOne = ""; string storeToDbValueTwo = "";
        for (int i = 0; i < countTimes; i++)
        {
            storeToDbValueOne += Request.Form["txtSkill" + i];

            storeToDbValueOne += Request.Form["txtVersion" + i];
        }

        Response.Write("<script>alert('"+storeToDbValueOne +" "+storeToDbValueTwo+"')</script>");


    }


Best regards
M.Mitwalli
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900