468,738 Members | 2,641 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

How to create Dynamic Controls

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
Nov 18 '05 #1
3 2776
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!
************************************************** ********************
"RSB" <rs*****@hotmail.com> wrote in message
news:yf***************@news.cpqcorp.net...
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

Nov 18 '05 #2
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;

}

}


"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:em*************@tk2msftngp13.phx.gbl...
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!
************************************************** ********************
"RSB" <rs*****@hotmail.com> wrote in message
news:yf***************@news.cpqcorp.net...
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


Nov 18 '05 #3
Call the dynamic control create method in the init method. So you won't face
any problems when doing a postback.
this is a good example from the msdn

http://support.microsoft.com/default...b;EN-US;317794

HTH

"RSB" <rs*****@hotmail.com> wrote in message
news:yf***************@news.cpqcorp.net...
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

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grey | last post: by
3 posts views Thread by sck10 | last post: by
reply views Thread by Slam via DotNetMonster.com | last post: by
15 posts views Thread by lxyone | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.