打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# – 不能在datalist中为CheckBoxList使用RequiredFieldValidator

我在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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
datalist绑定用法
在gridview中把行中多余的字符用省略号代替(C#)
ASP.NET数据绑定
ASP.NET 2.0中实现模板中的数据绑定
一天精通asp.net:专为有其他语言基础的人 (转)
DataList 嵌套 全选反选
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服