2 Replies - 1459 Views - Last Post: 30 September 2010 - 08:49 AM Rate Topic: -----

#1 weedweaver  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 42
  • Joined: 01-May 07

ASP.Net Dynamic controls and Validators

Posted 21 September 2010 - 07:43 AM

Hi All,
I am having a slight issue with dynamic controls and my ASP.net validators. I am creating controls on the fly (by user request) and adding them to the page. I.e. I have a button, which when they click, uses the LoadControl method and loads in my user control to the page. This all works fine, values are persisted to viewstate etc. and the controls are recreated on postback (so they do not get lost if they user clicks another button). In the markup for the dynamic control I am adding (it is just a simple ascx control with a few textboxes) I have my required field validators and regex validators, however, these never get fired on the dynamic control for any postback for that page.

I did a quick test and loaded the controls in OnInit using the exact same method and it works beautifully. I cannot do this in the my case though as I am relying on viewstate to hold the number of controls I have added plus their Id's (I use these if the user decides to remove the control) which hasn't been loaded at this stage in the page lifecycle.

I am guessing it is something to with the control being loaded onto the page after Page_load but I thought the LoadControl method called all the lifecycle methods for that control and it essentially 'plays catch up' with the page.

Any help would be greatly appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.Net Dynamic controls and Validators

#2 weedweaver  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 42
  • Joined: 01-May 07

Re: ASP.Net Dynamic controls and Validators

Posted 21 September 2010 - 12:45 PM

ok I have sorted it! on the next button click (or anything really that causes a postback) I force the page to validate by calling:

Page.Validate();
if (Page.IsValid)
{
 //carry on to the next stage
}



and then only continuing if the page is valid.
Was This Post Helpful? 0
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: ASP.Net Dynamic controls and Validators

Posted 30 September 2010 - 08:49 AM

You should be loading the controls during the OnInit event or else you could lose information (like validation information) concerning the controls within the dynamic controls....just as you have discovered.

I know you've solved your problem and that things are working well for you...but I would recommend storing the number controls in a Cookie or in a HiddenField instead of in ViewState. Both cookies and hiddenfields are available during the OnInit event (whereas ViewState is not).

-Frinny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1