The RequiredFieldValidator Control

RequiredFieldValidator Control"-->

only for RuBoard

The RequiredFieldValidator Control

The RequiredFieldValidator is by far the simplest validator control to use. The Required FieldValidator will find a control invalid if the value the user entered is equal to the InitialValue attribute. The InitialValue attribute is the value contained in the control that is to be validated (ex: TextBox ) when the page is first rendered to the client. If an InitialValue is not specified, then the value defaults to String.Empty (an empty string). For example, if you have a TextBox control with a RequiredFieldValidator and you don't set the InitialValue attribute, then the Page will not be valid until the user adds at least one character. A code example using the RequiredFieldValidator can be found in Listing 9.4.

Listing 9.4 Using the RequiredFieldValidator Control
 [VisualBasic.NET] 01: <script language="vb" runat="server"> 02: 03:  sub Validate_Page(sender as Object, e as EventArgs) 04: 05:   if (Page.IsValid) then 06: 07:     lblmessage.Text = "* Page Is Valid" 08: 09:   else 10: 11:     lblmessage.Text = "* Page Is Invalid" 12: 13:   end if 14: 15:  end sub 16: 17: </script> [C#.NET] 01: <script language="c#" runat="server"> 02: 03:  void Validate_Page(Object sender, EventArgs e) { 04: 05:    if (Page.IsValid) { 06: 07:     lblmessage.Text = "* Page Is Valid"; 08: 09:   }  else { 10: 11:     lblmessage.Text = "* Page Is Invalid"; 12: 13:   } 14: 15:  } 16: 17: </script> [VisualBasic.NET & C#.NET] 18: <html> 19:   <body Style="font-size:10"> 20:    <form runat="server"> 21:     <center> 22: 23:     <h3>RequiredFieldValidator</h3> 24:     <asp:Label runat="server" id="lblmessage" /> 25: 26:     <p> 27: 28:     <asp:TextBox 29:      id="txtFirstName" 30:      runat="server" 31:      Text="First Name" 32:     /> 33: 34:     <asp:RequiredFieldValidator 35:      id="rfv" 36:      runat="server" 37:      ControlToValidate="txtFirstName" 38:      Display="Dynamic" 39:      InitialValue="First Name" 40:     > 41:      * This is a Required Field 42:     </asp:RequiredFieldValidator> 43: 44:     <br> 45: 46:     <asp:Button id="SubmitButton" 47:      runat="server" 48:      Text="Validate" 49:      OnClick="Validate_Page" 50:     /> 51: 52:    </center> 53:   </form> 54:  </body> 55: </html> 

The RequiredFieldValidator in Listing 9.4 is on lines 34 “42 and is wired to the TextBox on lines 28 “32. The Button on lines 46 “50 is used to submit the page for server-side validation. You'll notice that the TextBox has an initial value of "First Name" (line 31) and because the TextBox control will only be valid once the initial value has changed, this value must somehow change. I used the InitialValue attribute (line 39) of the RequiredFieldValidator to wire gain this functionality. If I were not to use the InitialValue attribute, the page could be posted back to the server with "First Name" as the TextBox value. Figure 9.1 contains an image of the page on the first request. Notice the TextBox has "First Name" for a value.

Figure 9.1. The page on first request.
graphics/09fig01.gif

Try to submit the page without changing the value of the TextBox . The result will be that the page will not be submitted. Now change the value of the TextBox and try resubmitting the page. You'll receive a page similar to Figure 9.2.

Figure 9.2. The page is valid and submitted to the server.
graphics/09fig02.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net