在datalist中使用必填字段验证器时出现问题
我们有4个民意调查。在那个用户必须回答两个必要的民意调查。
我已经在我的datalist中添加了所有四个民意调查,它显示正常,并且在单击提交按钮时,所需的字段验证器正在工作并提供错误消息。一旦我们回答了所需的民意调查并再次点击提交按钮,所有其他不需要的民意调查显示为必需的民意调查。
我的民意调查答案提交不是问题,我想避免显示非必需民意调查的错误消息。
请帮我解决这个问题
这是我的设计和代码:
<div id="pollQuestion_div" Class="read-messages-poll" runat="server" >
<asp:DataList ID="PollDataList" runat="server" onitemdatabound="PollDataList_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="PollIDReqHiddenField" Value='<%# Eval("PollID") %>' runat="server" Visible="false" />
<asp:Label ID="lblReqQuestionNumber" runat="server" Text='<%# Eval("No of PollQuestion") %>' Font-Bold="true"></asp:Label><br />
<asp:Label ID="lblRequiredPollQusetion" runat="server" Text='<%# Eval("PollQuestions") %>' Font-Bold="true"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorReqPoll" runat="server" ControlToValidate="rdblstRequiredPollOptions" Display="Dynamic" ErrorMessage="*" ForeColor="Red" ></asp:RequiredFieldValidator>
<asp:HiddenField ID="HiddenFieldPollOption" runat="server" Value='<%# Eval("PollOptions") %>' Visible="false" />
<asp:HiddenField ID="HiddenFieldPollType" runat="server" Value='<%# Eval("PollType") %>' Visible="false"/>
<asp:RadioButtonList ID="rdblstRequiredPollOptions" runat="server" > </asp:RadioButtonList>
<asp:RadioButtonList ID="rdblstNonRequiredPollOptions" runat="server" > </asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxListMultiple" runat="server" RepeatDirection="Vertical"></asp:CheckBoxList>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnSubmitPoll" runat="server" Text="Submit" CssClass="custom-button" OnClick="btnSubmitPoll_click" />
</FooterTemplate>
</asp:DataList>
</div>
Datalist Item bound Code:
<div id="pollQuestion_div" Class="read-messages-poll" runat="server" >
<asp:DataList ID="PollDataList" runat="server" onitemdatabound="PollDataList_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="PollIDReqHiddenField" Value='<%# Eval("PollID") %>' runat="server" Visible="false" />
<asp:Label ID="lblReqQuestionNumber" runat="server" Text='<%# Eval("No of PollQuestion") %>' Font-Bold="true"></asp:Label><br />
<asp:Label ID="lblRequiredPollQusetion" runat="server" Text='<%# Eval("PollQuestions") %>' Font-Bold="true"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorReqPoll" runat="server" ControlToValidate="rdblstRequiredPollOptions" Display="Dynamic" ErrorMessage="*" ForeColor="Red" ></asp:RequiredFieldValidator>
<asp:HiddenField ID="HiddenFieldPollOption" runat="server" Value='<%# Eval("PollOptions") %>' Visible="false" />
<asp:HiddenField ID="HiddenFieldPollType" runat="server" Value='<%# Eval("PollType") %>' Visible="false"/>
<asp:RadioButtonList ID="rdblstRequiredPollOptions" runat="server" > </asp:RadioButtonList>
<asp:RadioButtonList ID="rdblstNonRequiredPollOptions" runat="server" > </asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxListMultiple" runat="server" RepeatDirection="Vertical"></asp:CheckBoxList>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnSubmitPoll" runat="server" Text="Submit" CssClass="custom-button" OnClick="btnSubmitPoll_click" />
</FooterTemplate>
</asp:DataList>
</div>
答案 0 :(得分:0)
在PollDataList_ItemDataBound中,您对项目进行迭代,并使用条件设置验证器的Enabled
属性
//禁用验证器
var validator = (RequiredFieldValidator )e.Item.FindControl("RequiredFieldValidatorReqPoll");
validator.Enabled=false;
//启用
var validator = (RequiredFieldValidator )e.Item.FindControl("RequiredFieldValidatorReqPoll");
validator.Enabled=true;