只有选中复选框且字段为空时,才会触发必填字段验证程序 [英] required field validator should fire only when checkbox is checked and fields are blank
问题描述
折叠|复制代码
场景
1)两个日历控件[fromdate和todate] ......两者都在开始时被禁用
2)复选框
3)保存按钮
- 如果我点击复选框...已启用已禁用的日历控件...然后我们输入并显示
- 如果我保存它应该更新
- <<<<<<<<< what i =require =>> ;.
Collapse | Copy Code
scenario
1)two calendar control[fromdate and todate]...both are disabled at start
2)checkbox
3)save button
--if i click the check box...the disabled calendar control becomes enabled...then we enter from and todate
--then if i save it should update
--<<<<<<<<<what i="" require="">>.
if i dont click the check box[that is both fromdate and todate are disabled and are left blank]. and then save it should save.
--problem
if i dont click the check box and when i am trying to save..both the calendar control required field validator message is firing..enter valid value in the fields
--i want these message to be displayed[on save button click] only when check box is clicked..and calendar control [both from and to date] is left blank.
----------- ----------- aspx代码------------------------------------- ----------
----------------------aspx code-----------------------------------------------
<tr>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<td align="left" class="style2" colspan="4">
<asp:CheckBox ID="chkroleid" runat="server" Text="Assign Role For Particular Duration"
OnCheckedChanged="chkroleid_CheckedChanged" Checked="false" AutoPostBack="true" />
</td>
</ContentTemplate>
</asp:UpdatePanel>
</tr>
<tr id="FirstRow" runat="server">
<td class="lavel">
<asp:Label ID="ll_Appointment_Date" runat="server" Text="From Date"></asp:Label>
</td>
<td width="25%">
<uc3:CalendarControl ID="txt_Appointment_From_Date" runat="server" ShowTime="true"
SetEnabled="false"/>
</td>
<td class="label" width="10%">
<asp:Label ID="Label2" runat="server" Text="To Date"></asp:Label>
</td>
<td width="25%">
<uc3:CalendarControl ID="txt_Appointment_To_Date" runat="server" ShowTime="true"
SetEnabled="false" />
</td>
</tr>
- 自定义按钮面板正在保存按钮 -
--custom button panel is having save button--
<uc1:CustomButtonPanel ID="CustomButtonPanel1" align="right" runat="server" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You must enter a valid value in the following field(s):"
ShowMessageBox="True" ShowSummary="False" />
---------------------------------------- -----------------------------------------------
所以如何解决这个问题?
Brij-
我有点困惑。你能帮助我吗 ?
- 用户control.ascx我有这个
< asp:RequiredFieldValidator ID = Date_rfvrunat =serverSetFocusOnError =trueControlToValidate =txtCalendarValue
Display =Dynamic>
- 在usercontrol.ascx.cs
public void SetDateValidators()
{
if(this.RfvDateErrorMsg。修剪()。长度> 0)
{
Date_rfv.ErrorMessage = this.RfvDateErrorMsg.ToString();
}
else
{
Date_rfv.ErrorMessage = System.Configuration.ConfigurationSettings.AppSettings [Date_rfv];
}
}
- 在web配置中声明消息[输入有效日期]
< add key =Date_rfvvalue =输入所需的日期值。>
------------------- ----------- --------------
所以当没有选中复选框时。
然后下面的验证开火
和webconfig关键信息
---------------------------------------------------------------------------------------
so how to solve this?
Brij-
I m little confused. Can you help me ?
--in user control.ascx i have this
<asp:RequiredFieldValidator ID="Date_rfv" runat="server" SetFocusOnError="true" ControlToValidate="txtCalendarValue"
Display="Dynamic">
-- in usercontrol.ascx.cs
public void SetDateValidators()
{
if (this.RfvDateErrorMsg.Trim().Length > 0)
{
Date_rfv.ErrorMessage = this.RfvDateErrorMsg.ToString();
}
else
{
Date_rfv.ErrorMessage = System.Configuration.ConfigurationSettings.AppSettings["Date_rfv"];
}
}
--in web config the message is declared [enter valid date]
<add key="Date_rfv" value="Enter required date value.">
--------------------------------------------
so when checkbox is not checked.
then the validation below fires
and the the webconfig key message
--custom button panel is having save button--
<pre lang="xml"><uc1:CustomButtonPanel ID="CustomButtonPanel1" align="right" runat="server" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You must enter a valid value in the following field(s):"
ShowMessageBox="True" ShowSummary="False" /></pre>
i得到如下错误
您必须在以下字段中输入有效值
输入所需日期值
输入所需日期值
- 在阅读你的文章之后我试图做了
i我试图在客户端进行验证..
< asp:CheckBox ID =chkroleidrunat =serverText =为特定持续时间分配角色
OnCheckedChanged =chkroleid_CheckedChanged Checked =falseOnclick =clientValidation_Calendarcontrol(); AutoPostBack =true/>
i get error like below
You must enter a valid value in the following field
Enter required date value
Enter required date value
--after reading your article i tried to do
i am trying to do validation on client side..
<asp:CheckBox ID="chkroleid" runat="server" Text="Assign Role For Particular Duration"
OnCheckedChanged="chkroleid_CheckedChanged" Checked="false" Onclick="clientValidation_Calendarcontrol();" AutoPostBack="true" />
function clientValidation_Calendarcontrol(source, arguments)
{
if (chkroleid.checked)
{
}
else
{
var ValidationSummary1 = document.getElementById('ValidationSummary1');
ValidatorEnable(ValidationSummary1, false);
ValidationSummaryOnSubmit();
}
}
- - 但这不起作用,,,
如何禁用验证摘要[必须在以下字段中输入有效值]
和usercontrol中声明的与webconfig链接的部分[输入所需日期值]
- kindly help
--but this is not working,,,
how to disable the validation summary [You must enter a valid value in the following field]
and the part that is declared in usercontrol which is linked with webconfig[Enter required date value]
--kindly help
推荐答案
所以有两种方法,无论你想从客户端还是从服务器端禁用它们。如果你想从服务器端做,那么在复选框上检查eent做回帖或使用ajax并启用/禁用验证。
你可以从客户端进行。从javascript禁用它。我已经写了一篇关于ASP.NET Validators的详细文章
你可以看看探索ASP.NET验证器 [ ^ ]
So there are two ways , whther you want to disable from client side or from server side. If you want to do from server side then on checkbox checked eent do a post back or use ajax and enable/disable the validation.
You can do it from Client side. Disable it from javascript. I have written a detailed article on ASP.NET Validators
You can have a look Exploring ASP.NET Validators[^]
这篇关于只有选中复选框且字段为空时,才会触发必填字段验证程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!