Dynamic validators and controls hookup in ASP.NET Ajax

In one of our screens, we have a list of items on the left pane and “details” screen on the right pane. The details pane appears only when one of the items is selected. If no item is selected – we show some sort of empty pane that describes the usability of the screen and ask the user to select an item in order to continue.

In order to switch between the details\empty panes, I draw two span elements and played with their visibility property. The controls are partially rendered which means that if the user did not select an item, no “details” controls or rendered and neither their validators. The problem is that ASP.NET register the validators only once (via ValidatorOnLoad() method) and on the first time the screen is rendered, the controls are not there yet (no item is selected by default).

The solution was pretty straight-forward, I think. I’ve downloaded a simple call to my javascript method so the validators will be re-attached again:

ScriptManager.RegisterStartupScript(this, this.GetType(), “HookupManuallyToValidators”, “ReRegisterValidators();”, true);

And the javascript method:

function ReRegisterValidators()
   if (typeof(Page_Validators) == “undefined”)

   var i, val;
   for (i = 0; i < Page_Validators.length; i++) 
        val = Page_Validators[i];
        ValidatorEnable(val, true);

1-0 to lnbogen .vs. asp.net


Oren Ellenbogen