473,242 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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 2950
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
3
by: Grey | last post by:
I know i can use placeholder control for dynamic create controls. but my requirement is i need to create multiple controls. when the user click the button, one textbox will be created. If user...
3
by: sck10 | last post by:
Hello, When a person subits my web page, I do a simple check (validation) and if there are any errors, I create a variable (strValidateText) and set that value to a label on the form. What I...
0
by: Slam via DotNetMonster.com | last post by:
I want to create a couple of dynamic controls and the controls definitions are stored in a database. Once the the controls are created, I want to fill any dropdownlist with database values. And...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.