c# - 不能对数据列表中的 CheckBoxList 使用 RequiredFieldValidator

标签 c# asp.net validation jquery-validate

我在为 Datalist 中的 CheckBoxList 使用 RequiredField Validator 时遇到问题。我正在使用复选框列表作为投票选项。我希望用户回答所需的投票问题。如果用户没有回答,我想显示一条错误消息。谁能帮我做这件事?

这是我的设计:

     <div id="divPollDataList">
     <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>
                <asp:Label ID="lblRequiredPollQusetion" runat="server" Text='<%# Eval("PollQuestions") %>' Font-Bold="true"></asp:Label>
                <asp:HiddenField ID="HiddenFieldPollOption" runat="server" Value='<%# Eval("PollOptions") %>' Visible="false" />
                <asp:HiddenField ID="HiddenFieldPollType" runat="server" Value='<%# Eval("PollType") %>' Visible="false"/>
               <asp:RequiredFieldValidator ID="RequiredFieldValidatorReqPoll" runat="server"  CausesValidation="true" ControlToValidate="CheckBoxListMultiple"  Display="Dynamic" ErrorMessage="You must provide the feedback" ></asp:RequiredFieldValidator>
                  <asp:CheckBoxList ID="CheckBoxListMultiple" runat="server" ></asp:CheckBoxList>
              </ItemTemplate>
</asp:DataList>

</div>
    <div>
       <asp:Button ID="btnSubmitPoll" runat="server" CausesValidation="true" Text="Submit"  OnClick="btnSubmitPoll_click"  />   

    </div>

<div>
    <asp:Button ID="btnBindData" runat="server" Text="Bind" 
        onclick="btnBindData_Click" />
</div>

这是我的代码:

   protected void PollDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Data.DataRowView drv = (System.Data.DataRowView)(e.Item.DataItem);

            string strPollID = drv.Row["PollID"].ToString();
            string pollOptions = drv.Row["PollOptions"].ToString();
            string strPollType = drv.Row["PollType"].ToString();
            string strPollRequiredorNot = drv.Row["RequiredPoll"].ToString();

                CheckBoxList chkList = (CheckBoxList)e.Item.FindControl("CheckBoxListMultiple");
                foreach (string opt in pollOptions.Split('}'))
                {
                    chkList.Items.Add(opt.ToString());
                }
                var validator = (RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidatorReqPoll");
                validator.Enabled = true;
        }
    }

最佳答案

您可以阅读这篇文章,它对 Validator 和 CheckBoxList 非常有趣。

您可以开发自定义验证器

public class RequiredFieldValidatorForCheckBoxList : BaseValidator
{
   //..code..
}

RadioButtonList 可以被验证,但 CheckBoxList 不能被验证

链接:http://www.codeproject.com/Articles/28560/CheckBoxList-Validation-in-ASP-Net-Required-Field

关于c# - 不能对数据列表中的 CheckBoxList 使用 RequiredFieldValidator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351180/

相关文章:

asp.net - Resx 文件在可移植类库中无法正常工作

ruby-on-rails - 在 Rails 中验证跨两个表的唯一性时如何避免竞争条件

c# - 验证摘要外观

c# - ASP.NET 和 C# 中的验证

c# - 在意外的时间调用了一个方法。 (HRESULT : 0x8000000E) exception was thrown by a method 的异常

c# - List<> 元素是否像数组一样按顺序位于堆中?

validation - knockout 验证 isValid() 不起作用

C# 泛型方法作为带有 where 子句的扩展

css - 如何使用响应式 bootstrap css 设置 asp.net 菜单控件的样式

Asp.net成员(member)密码格式