how can I generate and display in a webform textboxes on the fly ???

  • Thread starter Thread starter genc_ymeri
  • Start date Start date
G

genc_ymeri

Hello over there,
I would like to generate the textboxes on the fly depending on the number of
coulmns in a returning dataset.

I tried something like this :
TextBox temp = new TextBox();
MyPage.Controls.Add(temp);
// <-- the above one failed saying I should specify [run at server] when
this
is already a server comp

Another problem I see is that how can I specify where in the form they
should be displayed ??


Any tip will be very much appreciated,
Thank you in advace,

Genc Ymeri.
 
Generating controls dynamically can be a little tricky. All ASP.NET
controls must reside in a form control. This is rendered for you
automatically (you can see it in the html markup). When you add a
control to the page, you're actually adding it at the end of the
control tree- outside of all other controls, including the form tag.

What you want to do is add a generic control- like a placeholder- to
the page, and then add your textboxes to the placeholder.

placeholder1.Controls.Add(temp);

THis will esnure you're putting it inside the form tag, and it will
also allow you to specify where they show up on the page (because
they'll appear in the placeholder).

As a general best practice, you should always set the ID property of
the textbox.

Secondly, and this is the trickiest part- when you do a postback, you
must make sure all controls are added back to the page in the Init
event. Otherwise, you'll get all sorts of wierd issues like viewstate
errors, etc. If you add three textboxes to the page, send it to the
client- it has to come back the same way. (And by come back, I mean be
constructed in the same way in the Init event).
 

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