This documentation is archived and is not being maintained.

RequiredFieldValidator.InitialValue Property

Gets or sets the initial value of the associated input control.

[Visual Basic]
Public Property InitialValue As String
[C#]
public string InitialValue {get; set;}
[C++]
public: __property String* get_InitialValue();
public: __property void set_InitialValue(String*);
[JScript]
public function get InitialValue() : String;
public function set InitialValue(String);

Property Value

A string that specifies the initial value of the associated input control. The default is String.Empty.

Remarks

Use this property to specify the initial value of the input control.

Validation fails if and only if the value of the associated input control matches this InitialValue upon losing focus.

Note   The strings in both the InitialValue property and the input control are trimmed to remove extra spaces before and after the string before validation is performed.

Example

[Visual Basic, C#] The following example demonstrates how to use the InitialValue property to ensure that the user enters a value in the text box.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<body>
 
   <form runat="server">

      <h3>RequiredFieldValidator InitialValue Example</h3>
 
      Name: 
      <asp:TextBox id="Text1" 
           Text="Enter a value" 
           runat="server"/>
 
      <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
           InitialValue="Enter a value" 
           ControlToValidate="Text1"
           ErrorMessage="Required field!"
           runat="server"/>
 
      <p>
         
      <asp:Button id="Button1"
           Text="Validate" 
           runat="server"/>
 
   </form>
 
</body>
</html>

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<body>
 
   <form runat="server">

      <h3>RequiredFieldValidator InitialValue Example</h3>
 
      Name: 
      <asp:TextBox id="Text1" 
           Text="Enter a value" 
           runat="server"/>
 
      <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
           InitialValue="Enter a value" 
           ControlToValidate="Text1"
           ErrorMessage="Required field!"
           runat="server"/>
 
      <p>
         
      <asp:Button id="Button1"
           Text="Validate" 
           runat="server"/>
 
   </form>
 
</body>
</html>

[Visual Basic, C#] The following example demonstrates how to use the InitialValue property to ensure that the user selects an item from the list box.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<body>

   <form runat="server">

      <h3>RequiredFieldValidator InitialValue Example</h3>

      <asp:ListBox id="list"
           runat="server">
       
         <asp:ListItem Value="Australia">Australia</asp:ListItem>
         <asp:ListItem Selected="True" Value="NoCountry">--ChooseCountry--</asp:ListItem>
         <asp:ListItem Value="USA">USA</asp:ListItem>
       
      </asp:ListBox>
 
      <asp:RequiredFieldValidator id="valList"
           ForeColor="#FF0000"
           ErrorMessage="Selection Invalid!"
           ControlToValidate="list"
           InitialValue="NoCountry"
           EnableClientScript="False"
           runat="server"/>

      <p>

      <asp:Button id="Button1"
           Text="Submit"
           runat="server"/>

   </form>
 
</body>
</html>

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<body>

   <form runat="server">

      <h3>RequiredFieldValidator InitialValue Example</h3>

      <asp:ListBox id="list"
           runat="server">
       
         <asp:ListItem Value="Australia">Australia</asp:ListItem>
         <asp:ListItem Selected="True" Value="NoCountry">--ChooseCountry--</asp:ListItem>
         <asp:ListItem Value="USA">USA</asp:ListItem>
       
      </asp:ListBox>
 
      <asp:RequiredFieldValidator id="valList"
           ForeColor="#FF0000"
           ErrorMessage="Selection Invalid!"
           ControlToValidate="list"
           InitialValue="NoCountry"
           EnableClientScript="False"
           runat="server"/>

      <p>

      <asp:Button id="Button1"
           Text="Submit"
           runat="server"/>

   </form>
 
</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

RequiredFieldValidator Class | RequiredFieldValidator Members | System.Web.UI.WebControls Namespace

Show: