Addding usercontrol dynmaically with postback capability

N

news.microsoft.com

Hello,

I am trying to add an asp.net user control dynamically with the postback
capability.

Adding a user control seems straightfroward but making postback work is a
problem. When I say postback, I mean postback handled inside the
usercontrol.
e.g. let's say your user control has a text and a button. You save the text
property in viewstate varible so that it is preserved during postback. You
assign the new property value from textbox to viewstate variable after
postback.

Problem comes when controls are created dynamically.

Any help will be appreciated to make this work.

regards,
Pravin
 
M

Mark Fitzpatrick

What event do you dynamically add the user control? The main issue, the user
control must maintain the same id from postback to postback. If you add it
too late, it actually gets added after the postback event so none of the
click event gets fired. You have to add the control early. What I usually do
is add the control in the OnLoad or OnPreLoad event. Then if something
changes where I have to swap the control, I do it in the OnPreRender event
after wiping out the previously added control.

Another key issue, the id. Controlling the ID of the dynamically added
control will help you maintain consistancy in the postback events.

Hope this helps,
Mark Fitzpatrick
 
G

Gregory A. Beamer

news.microsoft.com said:
Hello,

I am trying to add an asp.net user control dynamically with the postback
capability.

Adding a user control seems straightfroward but making postback work is a
problem. When I say postback, I mean postback handled inside the
usercontrol.
e.g. let's say your user control has a text and a button. You save the
text property in viewstate varible so that it is preserved during
postback. You assign the new property value from textbox to viewstate
variable after postback.

Problem comes when controls are created dynamically.

Any help will be appreciated to make this work.

Mark has hit this on the head. In order to work with viewstate, you have to
have the control on the page prior to when viewstate is applied back, which
means before the Load event. Take a look at the page lifecycle events and
you will understand why adding the control on load completely messes up your
system.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

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

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top