Friday, January 13, 2006

SetFocusOnError property in ASP.NET 2.0

In ASP.NET 1.x, if we want to put the focus on a control if a validation fails based on the validation control, we need to tweak around webUiValidation.Js and modify it to work for us. Now in ASP.NET 2.0, we got a couple of options to do it. We got a SetFocus method as a part of Page in which we can pass the id of the control to get focus,

 Page.SetFocus(txtName);

we got a new property called SetFocusOnError which is set on validator controls to cause the first invalid control to receive focus.

<asp:TextBox ID="txtRollId" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator SetFocusOnError="true"
ErrorMessage="RollId is empty" ID="RequiredFieldValidator2"
ControlToValidate=" txtRollId" Display="Dynamic"
runat="server">*
</asp:RequiredFieldValidator>
Check out the SetFocusOnError = true set in requiredFieldvalidator
 

1 comment:

Gayani Devapriya said...

This is useful...Thank you...