How to create Dynamic Controls

  • Thread starter Thread starter RSB
  • Start date Start date
R

RSB

Hi Every one

Having tuff time creating web controls Dynamically. All i am trying to do is
read a table and generate a list of ASP TEXT Box.
So how do i create this Control dynamically and where do i add the
EventHandler to it.

Thanks
RSB
 
Adding controls is done, by one means, by adding controls to a container.
For example, if you have a panel, you can add controls like so:

pnlMyPanel.Controls.Add(MyTextBox);

You will have to set the control up with the properties set up correctly, of
course. You can also dynamically create the table.

Events can be added in the same way::

protected void MyControl_Changed(object sender, EventArgs e)
{
}

To use this event:

1. Add a handler on the fly

MyTextBox.TextChanged += new System.EventHandler(this.MyControl_Changed);

You can then test the sender value to find which control submitted and
handle accordingly. All events (at least I "think ALL) in .NET are handled
like so:

protected void EventHandler(object sender, SomeTypeOfArgs e)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Gregory is you're right about adding dynamic control along with an event
handler. One thing I must point out is--one must create the control on each
postback. If you create the controls in the !IsPostBack block in the
Page_Load event it is not going to work on postback.

Here is a function that I have created to create controls at runtime.
Controls are created within a place holder called phFields and controls are
created in a 2 columns(you can change it) table. I have added few attributes
to the TextBox because they become handy to identify the TextBox later on.
Hope this help......

Prodip Saha
private void CreateControls(DataTable dtFields)

{

try

{

int NumOfFields=0;

HtmlTableRow tr;

HtmlTableCell td;

TextBox tb;

Label l;

//Create HTML Table

HtmlTable t = new HtmlTable();

t.ID = "FieldTable";

t.Align = "center";

t.Border = 2;

t.BorderColor="Blue";

t.CellPadding = 5;


tr = new HtmlTableRow();

NumOfFields=1;

foreach (DataRow dr in dtFields.Rows)

{


td = new HtmlTableCell();

td.InnerHtml = dr["DISPLAY_NAME"].ToString();

tr.Cells.Add(td);

td = new HtmlTableCell();

if (dr["EDITABLE_YN"].ToString()=="Y")

{

tb = new TextBox();

//tb.ID = dr["FIELD_CD"].ToString();

tb.ID = dr["TABLE_NAME"].ToString() +"_" +dr["FIELD_CD"].ToString();

tb.AutoPostBack = true;

tb.Attributes.Add("TableName",dr["TABLE_NAME"].ToString());

tb.Attributes.Add("FieldName",dr["FIELD_CD"].ToString());

tb.Attributes.Add("DisplayName",dr["DISPLAY_NAME"].ToString());

tb.Width = 150;


//Attach an event handler to this control

tb.TextChanged += new System.EventHandler(this.FieldChanged);

td.Controls.Add(tb);

}

else

{

l = new Label();

l.ID = dr["TABLE_NAME"].ToString() +"_" +dr["FIELD_CD"].ToString();

l.Attributes.Add("TableName",dr["TABLE_NAME"].ToString());

l.Attributes.Add("FieldName",dr["FIELD_CD"].ToString());

l.Attributes.Add("DisplayName",dr["DISPLAY_NAME"].ToString());

td.Controls.Add(l);

}

tr.Cells.Add(td);



if((NumOfFields % 2)==0 & NumOfFields !=1) //2 TD per TR

{

t.Rows.Add(tr);

tr = new HtmlTableRow();

}

else if(NumOfFields==dtFields.Rows.Count)

{

t.Rows.Add(tr); //Last Row

}


NumOfFields +=1;

}

phFields.Controls.Add(t);

}

catch(Exception ex)

{

throw ex;

}

}
 
Back
Top