Adding dynamically user control (ASCX) into asp.net page and handling OnClick event

F

Frank

Hi,

I've been reading a lot's of Q&A on user controls but none seem to
answer my question. Here it is. I need to add dynamically a user
control, in a htmltablecell, in an asp.net page (done in VB). This is
the way, that I thought, was the wright way doing it:

Dim oTable As New HtmlTable()
Dim oRow As New HtmlTableRow()
Dim oCell As New HtmlTableCell()
Dim uc As Control = Page.LoadControl(ControlName)

oTable = New HtmlTable()
oRow = New HtmlTableRow()
oCell = New HtmlTableCell()
oCell.Controls.Add(uc)
oRow.Controls.Add(oCell)
oTable.Controls.Add(oRow)

Return oTable

The control I try to load contain two ASP:TextBox and one ASP:Button,
this is a login user control to access a restricted section of the web
site.

The control loads fine in the asp.net page when I do it that way, but
when I hit the button, the page do not trigger the event and do only a
postback. Can someone tell me what's missing in the control page and
also in the asp.net page ? Do I need also to add something to handle
postback event for the data in the textbox ?

Thanks

Frank
 
A

Alessandro Zifiglio

hi frank, you need to add the control even after a postback, it will
maintain state so you need not worry about losting the state it was in
before the postback just coz you are adding the control again.

Make sure you are loading your control and adding it to you table even after
the page is posting back ;)
 
V

Victor Garcia Aprea [MVP]

Hi Frank,

At which event are you adding the UC?

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top