触发验证时发出单选按钮问题

时间:2011-09-28 09:21:47

标签: c# asp.net

大家好我的表格有一些控件如下

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>

仅在启用控件时才应用验证

2 个答案:

答案 0 :(得分:1)

验证程序具有可以使用的Enabled属性:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    TextBox1.Enabled = RequiredFieldValidator1.Enabled = true;
}

protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
    TextBox1.Enabled = RequiredFieldValidator1.Enabled = false;
}

答案 1 :(得分:0)

我得到了这个并且适合我

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    TextBox1.Enabled = true;
    Button1.CausesValidation = true;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
    Button1.CausesValidation = false;
    TextBox1.Enabled = false;
}