Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
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("&nbsp"));
                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("&nbsp&nbsp"));
            cell1.Controls.Add(ddl);
            cell1.Controls.Add(new LiteralControl("<br /><center>"));
            cell1.Controls.Add(btn);
            row1.Cells.Add(cell1);
            table.Rows.Add(row1);
        }
    }
Posted 27-Mar-12 22:54pm
Edited 30-Apr-12 2:42am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 .
 

<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 :
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
  Permalink  
v2
Comments
shalu verma at 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 at 26-Dec-12 15:40pm
   
Hi shalu i'm sorry I didn't get what you want .
Member 10021652 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

{
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;
}
}
  Permalink  

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

  Print Answers RSS
0 CPallini 330
1 Sergey Alexandrovich Kryukov 313
2 George Jonsson 291
3 Prasad Avunoori 155
4 OriginalGriff 149
0 OriginalGriff 4,623
1 CPallini 3,410
2 Sergey Alexandrovich Kryukov 2,929
3 George Jonsson 2,319
4 Gihan Liyanage 2,077


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100