Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more: , +
Hai,
How to create dynamic textbox,dropdownlist in asp.net and how to save that values in database?
Please check below code once:all controls are add but nw i want save that values in database.How to save that values in database?Genaral code is not working for save control.
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("<br />"));
                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("<br /><center>"));
            cell1.Controls.Add(btn);
            row1.Cells.Add(cell1);
            table.Rows.Add(row1);
        }
    }
Posted
Updated 30-Apr-12 2:42am
v3

HI , 
I have improved my solution, 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
 
v2
Comments
shalu verma 26-Dec-12 6:18am    
in your page you have taken seven fields like home,articles,quick answers,discussions,features,community,help,I want to what control it is.either it is a menu control,a dropdownlist or something else????
Mohamed Mitwalli 26-Dec-12 15:40pm    
Hi shalu i'm sorry I didn't get what you want .
Member 10021652 17-May-14 5:21am    
I want to bind a column value in textbox. and As I enter value in textbox it show the related value in textbox
Hi Hari,

I would recommend you to go through this post to understand about creating and accessing controls dynamically


http://support.microsoft.com/kb/317794[^]
 
Share this answer
 
try this,
//page load
            TextBox txt = new TextBox();
            form1.Controls.Add(txt);
            DropDownList ddl = new DropDownList();
            List<string> list = new List<string>();
            list.Add("Item1");
            list.Add("Item2");
            list.Add("Item3");
            ddl.DataSource = list;
            ddl.DataBind();
            form1.Controls.Add(ddl);
            txt.ID = "txt";
            ddl.ID = "ddl";
            txt.Text = "test";

//button click event

 TextBox b = Page.FindControl("txt") as TextBox;
        if (b != null)
        {
            Response.Write("Found txt on Button1_Click<br>");
        }

        DropDownList d = Page.FindControl("ddl") as DropDownList;
        if (d != null)
        {
            Response.Write("Found ddl on Button1_Click<br>");
        }


see reference

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.dropdownlist(v=vs.71).aspx
http://www.codinghub.net/2011/06/how-to-find-all-controls-in-aspnet.html
http://odetocode.com/articles/116.aspx
 
Share this answer
 
v2
{ this.CreateTextBoxes();
this.CreateCheckBoxes();
this.CreateButtons();
}

protected void ddlTextBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
this.CreateButtons();
}

protected void btnTextBoxes_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("You entered:
");

foreach (Control control in phTextBoxes.Controls)
{
if (control is TextBox)
{
sb.AppendFormat("{0} - {1}
", control.ID, ((TextBox)control).Text);
}
}

lblTextBoxes.Text = sb.ToString();
lblTextBoxes.Visible = true;
}

protected void ddlCheckBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
this.CreateCheckBoxes();
}

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;

lblCheckBoxes.Text = chk.Checked ?
String.Format("You checked CheckBox '{0}'.", chk.ID) :
String.Format("You unchecked CheckBox '{0}'.", chk.ID);
lblCheckBoxes.Visible = true;
}

protected void ddlButtons_SelectedIndexChanged(object sender, EventArgs e)
{
this.CreateButtons();
}

protected void Button_Click(object sender, EventArgs e)
{
lblButtons.Text = String.Format("You clicked Button '{0}'.", ((Button)sender).ID);
lblButtons.Visible = true;
}
}
 
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