Adsence750x90

Wednesday, August 13, 2008

How to add Dynamic TextBox in ASP.NET C# - code samples


How to add TextBox Dynamically in ASP.NET C#
we can dynamically add controls in asp.net Pages. Hera i am explain how to add textbox control in our .aspx page. use PlaceHolder control for Holding the dynamically added control. Below am showing the C# code for generating textbox control. if u have any doubt on this article please comment below.


C# Code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// Function for Generating Dynamic Textbox
/// for creating TextBox Dynamically ,first create a Table then table Row then TableColumn
/// add TextBoxes in TableColumn with ID
/// Add TableColum in TableRow
/// Add TableRow In Table
/// Add that Table in PlaceHolder
public void GenarateTextBox(PlaceHolder PlaceHolder1, int RowCount, int ColCount)
{
PlaceHolder ph = new PlaceHolder();// PlaceHolder for Hold dynamically createing Text Box
Table objTable = new Table(); //
if (objTable.GetType().ToString().Equals("System.Web.UI.WebControls.Table") && PlaceHolder1.FindControl("objTable") == null)
{
objTable.ID = "objTable";
}
objTable.EnableViewState = true;
objTable.BorderWidth = Unit.Pixel(0);
objTable.CellPadding = 3;
objTable.CellSpacing = 0;
objTable.Width = Unit.Percentage(6);
TableCell objtableCell;
for (int j = 1; j <= RowCount; j++) 
{TableRow objTableRow = new TableRow();
for (int i = 1; i <= ColCount; i++)
 {  objtableCell = new TableCell();
    TextBox objTextBox = new TextBox();
if(objTextBox.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")&&(PlaceHolder1.FindControl("TextBox"+j+i)==null)) 
     {  objtableCell.HorizontalAlign = HorizontalAlign.Left;
        objtableCell.VerticalAlign = VerticalAlign.Middle;
 objtableCell.Height = Unit.Pixel(10); 
objTextBox.ID = "TextBox" + j + i;
objTextBox.Text = "TextBox" + j + i;
objTextBox.CssClass = "textarea1"; 
objTextBox.Width = Unit.Pixel(100); 
objtableCell.Controls.Add(objTextBox); 
objTableRow.Cells.Add(objtableCell);
 } 
 } 
objtableCell = new TableCell(); 
objTable.Rows.Add(objTableRow); 
} 
PlaceHolder1.Controls.Add(objTable);
}
protected void Button1_Click(object sender, EventArgs e) { 
GenarateTextBox(PlaceHolder1,Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox2.Text)); 
 }  } 

Download Source Code Complete C# ASP.NET source Code

6 comments:

Anonymous said...

nice article.hat off

Anonymous said...

good one..
cheers..

Anonymous said...

where the hell content placeholder is used again after creating object?....

Anonymous said...

if i create the ajax calendar control i am add one textbox and near to add one link button. First if i click the text box the calendar control is opened. that time i will select one date in the calendar control, the selected date will be show in the text box it will working. The another step is if i click the link button the text box is show in dynamically in the page that time i select the textbox viewing the calendar control the another date will be selected the date will be show in the dynamic text box. How it work sir. And reply me the sample coding and output sir please. my mail id is: nilofathi@gmail.com

Anonymous said...

good one...

Unknown said...

how to add variable number of controls on asp.net form when pressed on add button and delete the controls one by one when pressed on delete button