Adding Controls at runtime ?

  • Thread starter Thread starter Thomas H.
  • Start date Start date
T

Thomas H.

Hi folks,

what's the best way to add (web)controls at runtime ??

TIA

T.H.
 
You could add them at that page load or event handlers of buttons, check
boxes etc. whenever needed.

Add them in the form object:
HtmlForm f =Page.FindControl("form1");
f.Controls.Add(<Control>);

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
what's the best way to add (web)controls at runtime ??
Add them to the Controls collection of the parent object. Here is a
simple example of how you can add a Label control to the web form.

C#:
Label message=new Label();
message.Text="Hello World!";
Controls.Add(message);

VB.NET:
Dim message As New Label()
message.Text="Hello World!"
Controls.Add(message)

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
How can I do that with a textbox, I'll get an error saying there must
be positioned in a formtag with runat = "server", but where if I
generate in at runtime??
 
How can I do that with a textbox, I'll get an error saying there must
be positioned in a formtag with runat = "server", but where if I
generate in at runtime??
You'll have to add it to the HtmlForm control. The following C# code
will add a textbox to the form:
TextBox tbx=new TextBox();
tbx.Text="Hello";
Page.FindControl("Form1").Controls.Add(tbx);


Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Great, one last question, is the a chance to directly insert XML-Data
to that textbox ?
 
Great, one last question, is the a chance to directly insert XML-Data
to that textbox ?
The Text property of the TextBox hold the value displayed in the
textbox. Just assign the XML string to this property. If you have an
XmlDocument instance holding the XML data you can do this to display the
entire XML document in the TextBox:

myTextBox.Text=myXmlDocument.DocumentElement.OuterXml;

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Sorry my fault,

ich don't mean the whole XML document, but only dataparts of it

i.E.

a very smple xml.file

<Name>Peter</Name>

The nametag should be inserted in a label and the content into a
textbox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top