RequiredFieldValidator.InitialValue Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o valor inicial do controlo de entrada associado.
public:
property System::String ^ InitialValue { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string InitialValue { get; set; }
[System.Web.UI.Themeable(false)]
public string InitialValue { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.InitialValue : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.InitialValue : string with get, set
Public Property InitialValue As String
Valor de Propriedade
Uma cadeia que especifica o valor inicial do controlo de entrada associado. A predefinição é Empty.
- Atributos
Exemplos
O exemplo seguinte demonstra como usar a InitialValue propriedade para garantir que o utilizador insere um valor na caixa de texto.
Important
Este exemplo tem uma caixa de texto que aceita a entrada do utilizador, o que constitui uma potencial ameaça à segurança. Por defeito, as páginas Web do ASP.NET validam que a entrada do utilizador não inclui elementos de script ou HTML. Para mais informações, consulte Visão Geral dos Exploits de Scripts.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RequiredFieldValidator InitialValue Example</title>
</head>
<body>
<form id="form1" 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"/>
<br />
<asp:Button id="Button1"
Text="Validate"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RequiredFieldValidator InitialValue Example</title>
</head>
<body>
<form id="form1" 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"/>
<br />
<asp:Button id="Button1"
Text="Validate"
runat="server"/>
</form>
</body>
</html>
O exemplo seguinte demonstra como usar a InitialValue propriedade para garantir que o utilizador seleciona um item da caixa da lista.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RequiredFieldValidator InitialValue Example</title>
</head>
<body>
<form id="form1" 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"/>
<br />
<asp:Button id="Button1"
Text="Submit"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RequiredFieldValidator InitialValue Example</title>
</head>
<body>
<form id="form1" 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"/>
<br />
<asp:Button id="Button1"
Text="Submit"
runat="server"/>
</form>
</body>
</html>
Observações
Use esta propriedade para especificar o valor inicial do controlo de entrada.
A validação falha apenas se o valor do controlo de entrada associado corresponder a este InitialValue ao perder o foco.
Note
As strings tanto na InitialValue propriedade como no controlo de entrada são cortadas para remover espaços extra antes e depois da string antes da validação ser realizada.
Esta propriedade não pode ser definida por temas ou temas de folhas de estilo. Para mais informações, consulte ThemeableAttribute e ASP.NET Temas e Skins.