【问题标题】:Issue with radio buttons while firing Validations触发验证时单选按钮出现问题
【发布时间】: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 试试

标签: c# asp.net


【解决方案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;
}

【讨论】:

    【解决方案2】:

    我得到了这个并且对我很有效

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 2011-06-22
      • 2017-06-06
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多