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 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();
     {  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 = new TableCell(); 
protected void Button1_Click(object sender, EventArgs e) { 
 }  } 

Download Source Code Complete C# ASP.NET source Code


Anonymous said...

nice article.hat off

Anonymous said...

good one..

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:

Anonymous said...

good one...

Jagadeesh Naidu said...

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