Geeks With Blogs
Marcin Celej blog

Few days ago I worked on a UserControl that displays dynamic content depending on a workflow step (the workflow does not matter here as the control could display data dynamically returned from a Web Service or database or whatever there is in the developer's world). I had a big problem as the controls can be added to the parent control (UserControl here) only in OnInit event. Well, you can add them in any other event but when you do it you won't get the results gathered by the controls after there is a post-back. Here is a detailed scenario:

  1. UserControl is diplayed for the first time
  2. In the OnInit event there are controls added that are taken from a process step and they are rendered to the user
  3. User fills some data into controls (into Textbox) and clicks a button - the button represents a process transition between current step and the next one
  4. The button OnClick event gathers information from the TextBox and saves it somewhere (everything works fine here)
  5. In the OnClick event the process is moved to the next step
  6. Still in the OnClick event there is invoked the same method as in the step 2 which renders the controls dynamically depending on the process step
  7. The control is rendered to the user and here starts a problem - another TextBox is generated
  8. User enters some data in the TextBox (the one from the step 2 of the process) and clicks another button (button moves the process to a step 3). But in the second OnClick event there is no data available in the TextBox control. It looks like empty TextBox even though the user entered something there.

Firstly I resolved it by adding this.Response.Redirect(this.Request.RawUrl) but the page was refreshed each time the user clicked any button, which did not look nice as I use AJAX everywhere.

Secondly I used this.Server.Transfer(this.Request.RawUrl, true) and since then it worked fine. The page does not blink and everything is great.

I'm not sure if my solution is proper. Maybe I should change the above algorithm somehow. I don't know. It works fine for me and I didn't find side effetcs yet. I couldn't find a solution for the problem on the web so I post my solution here.

Posted on Tuesday, August 7, 2007 7:03 PM | Back to top

Comments on this post: Creating controls dynamically in ASP.NET in Click event

# re: Creating controls dynamically in ASP.NET in Click event
Requesting Gravatar...
Absolutely non-sense was ur solution ...... never ever dare to post such comments which dont work..................
I have been trying this solution for last 8 hours and i came 2 know its really fake like you
Left by Piyush on Jun 02, 2009 11:06 PM

Your comment:
 (will show your gravatar)

Copyright © Marcin Celej | Powered by: