locked
How to make a DynamicControl "required" at run-time? RRS feed

  • Question

  • User1475640560 posted

    This seems like it should be straightforward, but I'm drawing a blank.  The problem I have is that on some forms a field is required and on others it isn't.  That means I can't set "Required" in the model.  But I can't figure out how to do it at runtime.  I've tried looping through the controls in the FormView's OnCreated event and enabling the RequiredFieldValidator sub-control, but by then it is too late because SetUpValidator has already been called in the FieldTemplate's PageLoad (so the RequiredFieldValidator doesn't get wired up to the actual dynamic input control, I think).  At the same time, the IsRequired property of the columns in the MetaTable is read-only.

    Can anyone think of a way to switch on the Required attribute at the right point in time to get the RequiredFieldValidator to hook up properly without using the model?

    Thanks for any help!

    Richard
     

    Saturday, August 16, 2008 4:35 PM

All replies

  • User955742345 posted

    An easier solution would be to create a user control with the field and its validator.  You can then have a public property on the user control, i.e., "EnableValidation".  When this is changed, enable/disable the "Enabled" property on your validator.

    When creating the user control, give both the field and its validator fixed names, and set the validator to validate that field.  This will never change as you use your control; all you should need to change is that property mentioned above.

    Then just use your control in place of the original field on the page.  The page will still validate the validator in the user control as normal.  As long as the property is set after the LoadViewState and before the Render stages of the page lifecycle (i.e., Page_Load and Page_PreRender are ok), then the validator should emit the correct code to validate the field.

    Sunday, August 17, 2008 2:30 AM
  • User1475640560 posted

    This sort of takes us out of ASP.NET Dynamic Data and puts us back in the land of web forms, doesn't it? I thought about creating a separate set of field templates that would always turn on the "required" attribute, but even that seems like a brute-force approach.  What I can't figure out is, if UIHint and other properties from the model are overridable as attributes of the DynamicControl, why isn't "Required?" 

    Sunday, August 17, 2008 2:42 AM
  • User1641955678 posted

    I think doing this is going to require some changes to the field templates.  Note that when you put arbitrary attributes on the asp:DynamicControl, they get passed on to the field template if it has a property with a matching name.  e.g. look at AllowNavigation in ForeignKey.ascx.cs.  So if you were to add a ForceRequired property in the field templates, you would be able to pass it a value from the DynamicControl.  You could then do what you need with the validator based on the flag.  Or something along those lines...

    David

    Monday, August 18, 2008 3:42 PM