【发布时间】:2011-09-28 09:21:46
【问题描述】:
大家好,我的表单带有一些控件,如下所示
2 Radio buttons 1 Text Box, 1 Required field validator and a button
我编写示例代码的方式是,如果选择了一个单选按钮,我将启用或禁用我将拥有的文本框。
我有一个为可用文本框设置的必填字段验证器。现在我需要的是当控件被禁用时我不想执行验证是否可以这样做
示例代码
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = true;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = false;
}
我的设计
<form id="form1" runat="server">
<div>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="g"
OnCheckedChanged="RadioButton1_CheckedChanged" ValidationGroup="g1" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="g"
OnCheckedChanged="RadioButton2_CheckedChanged" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RequiredFieldValidator" ValidationGroup="g1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="g1" /></div>
</form>
只有在启用控件时才应用验证
【问题讨论】:
-
stackoverflow 本身有很多类似的问题。用这个google.co.in/…google 试试