无法在datalist中对CheckBoxList使用RequiredFieldValidator

时间:2012-09-10 11:56:30

标签: 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;
        }
    }

2 个答案:

答案 0 :(得分:2)

你可以阅读这篇文章,它非常关注Validator和CheckBoxList。

您可以开发自定义验证器

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

可以验证RadioButtonList,但不验证CheckBoxList

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

答案 1 :(得分:0)

无法使用RequiredFieldValidator验证CheckBoxList。您可能需要使用javascript或jQuery进行验证。