Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to create Dynamic multiple Textbox in asp.net using C#

Tags | how to add Textbox dynamically in asp.net c#, how to add textbox on button click in asp.net c#, how to create dynamic textbox on button click in asp.net c#, asp.net add controls dynamically on button click, how to create multiple textbox dynamically in c#, how to create controls dynamically in asp.net and retrieve values from it

  • Send Email with Attachment in ASP.Net using C#
  • jQuery Gridview Custom Control ASP.NET

How to create dynamic textbox on button click

At times it is more practical to create a control at runtime than at design time. For example, Create Question and Answers page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.

This asp.net c# tutorial demonstrates how to create controls dynamically in asp.net and retrieve values from it.

How to create dynamic textbox on button click in asp.net c#

In order to programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as a container, you can use a PlaceHolder or Panel Web server control. We are using Panel as a container.

For example: Create ASP.Net Quiz Application by adding questions and answers. So in this example, we will learn how to generate multiple textboxes dynamically. Find the source code below:-





    asp.net quiz application - how to create dynamic textbox on button click in asp.net c#

How to create controls dynamically in asp.net and retrieve values from it



Question Answer
  

using System; using System.Collections.Generic; using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; public partial class Demo : System.Web.UI.Page { protected void Page_PreInit(object sender, EventArgs e) { List keysQ = Request.Form.AllKeys.Where(key => key.Contains("txtQ")).ToList(); if (keysQ.Count > 0) { foreach (string key in keysQ) { int txtId = Convert.ToInt16(key.Substring(4)); this.CreateTextBoxQ("txtQ" + txtId, txtId); txtId++; } } List keysA = Request.Form.AllKeys.Where(key => key.Contains("txtA")).ToList(); if (keysA.Count > 0) { foreach (string key in keysA) { int txtId = Convert.ToInt16(key.Substring(4)); this.CreateTextBoxA("txtA" + txtId, txtId); txtId++; } } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { pnlQuestion.Controls.Clear(); pnlAnswer.Controls.Clear(); } } private void InitializeDynamicText() { int index = pnlQuestion.Controls.OfType().ToList().Count + 1; int index1 = pnlAnswer.Controls.OfType().ToList().Count + 1; this.CreateTextBoxQ("txtQ" + index, index); this.CreateTextBoxA("txtA" + index1, index1); } protected void AddTextBox(object sender, EventArgs e) { int indexQ = pnlQuestion.Controls.OfType().ToList().Count + 1; int indexA = pnlAnswer.Controls.OfType().ToList().Count + 1; this.CreateTextBoxQ("txtQ" + indexQ, indexQ); this.CreateTextBoxA("txtA" + indexA, indexA); } private void CreateTextBoxQ(string id, int i) { TextBox txt = new TextBox { ID = id, Width = 120 }; Label lbl = new Label { Text = "Question " + i + ": " }; Literal lt = new Literal { Text = "
" }; pnlQuestion.Controls.Add(txt); pnlQuestion.Controls.Add(lt); } private void CreateTextBoxA(string id, int i) { TextBox txt = new TextBox { ID = id, Width = 120 }; Label lbl = new Label { Text = "Answer " + ": " }; Literal lt = new Literal { Text = "
" }; Button btnRemove = new Button { ID = "bttn" + i.ToString(), Text = "x" }; btnRemove.Click += new EventHandler(Remove_Click); btnRemove.Visible = i != 1; pnlAnswer.Controls.Add(txt); pnlAnswer.Controls.Add(btnRemove); pnlAnswer.Controls.Add(lt); } protected void GetTextBoxValues(object sender, EventArgs e) { try { string resultQ = pnlQuestion.Controls.OfType().Aggregate("", (current, textBox) => current + (textBox.ID + ": " + textBox.Text + ", ")); string resultA = pnlAnswer.Controls.OfType().Aggregate("", (current, textBox) => current + (textBox.ID + ": " + textBox.Text + ", ")); lblResult.Text = resultQ + resultA; } catch (Exception) { throw; } } protected void btnAddNew_Click(object sender, EventArgs e) { divAdd.Visible = true; InitializeDynamicText(); } protected void Cancel_Click(object sender, EventArgs e) { divAdd.Visible = false; pnlQuestion.Controls.Clear(); pnlAnswer.Controls.Clear(); lblResult.Text = string.Empty; } protected void btnRemove_Click(object sender, EventArgs e) { List keysQ = Request.Form.AllKeys.Where(key => key.Contains("txtQ")).ToList(); List keysA = Request.Form.AllKeys.Where(key => key.Contains("txtA")).ToList(); int countQ = keysQ.Count; int countA = keysA.Count; if (countQ > 1 && countA > 1) { pnlQuestion.Controls.Remove(pnlQuestion.FindControl("txtQ" + countQ + "")); pnlAnswer.Controls.Remove(pnlAnswer.FindControl("txtA" + countA + "")); } } void Remove_Click(object sender, EventArgs e) { Button ib = sender as Button; if (ib != null) { string btnId = ib.ID; string txtId = btnId.Substring(4); string deltxtQ = "txtQ" + txtId; foreach (Control c in pnlQuestion.Controls) { if (c.ID == deltxtQ) { pnlQuestion.Controls.Remove(c); break; } } string deltxtA = "txtA" + txtId; foreach (Control a in pnlAnswer.Controls) { if (a.ID == deltxtA) { pnlAnswer.Controls.Remove(a); pnlAnswer.Controls.Remove(ib); break; } } } } }

Download Source Code

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

The post How to create Dynamic multiple Textbox in asp.net using C# appeared first on Dot Net Tutorial, C# Tutorial, Google Maps API JavaScript, Dot Net Tricks.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

How to create Dynamic multiple Textbox in asp.net using C#

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×