启用从CS页/禁用必填字段验证? [英] Enable/Disable Required field validator from cs page?

查看:189
本文介绍了启用从CS页/禁用必填字段验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,并在我的网页两个按钮。

I have two TextBox and two Buttons in my page.

一个是隐藏的,另一种是显示

One is hidden and the other one is displayed.

当我点击 Button1的,这将节省的两个文本框数据,并确认由每个文本框的RequiredFieldValidator

When I click the Button1, it will save data of the two TextBox and will validate each TextBox by the RequiredFieldValidator.

然后当我点击将Button2 ,它只是隐藏自身(将Button2 ),并会显示隐藏文本框

Then when I click Button2, it will just hide itself (Button2) and will show the hidden TextBox.

两者文本框的RequiredFieldValidator 验证对 Button1的 ■事件点击。

Both TextBox has RequiredFieldValidator validating against Button1's Event click.

现在我的问题是,当我简单地输入文本到文本框1,点击保存按钮单击正在验证所需的字段隐藏域。我只是想确认2文本框时,它显示。

Now my issue is when I simply enter text to the 1st TextBox and click save, the button click is validating the required field for hidden field. I just want to validate the 2 textbox when it is showing.

我怎样才能避免这种情况?

How can I avoid this?

推荐答案

那么你可以简单的使用启用=的假属性的RequiredFieldValidator

Well you can simple use the Enabled="false" property of RequiredFieldValidator.

标记看起来像此基础上你的问题。

Your markup would look something like this based on your Question.

<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="tb1" ErrorMessage="*" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:TextBox runat="server" ID="tb2" Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv2" ControlToValidate="tb2" ErrorMessage="*" Enabled="false" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:Button runat="server" ID="btn1" Text="Save" onclick="btn1_Click" ValidationGroup="gvSave"/>
<asp:Button runat="server" ID="btn2" Text="Show" onclick="btn2_Click" />

和您的 codebehind 是这样的:

protected void btn2_Click(object sender, EventArgs e)
{
    tb2.Visible = true;
    rfv2.Enabled = true; // Enables the second requiredfieldvalidator
}

protected void btn1_Click(object sender, EventArgs e)
{
  // your Saving code here
}

这篇关于启用从CS页/禁用必填字段验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆