C# .NET - Required field validator for radiobutton list in datalist is not working

Asked By Sudha Duraisamy on 13-Sep-12 02:40 AM

Hi,

I have a problem on using required field validator for radio button list and custom field validator for checkbox list.
I have some poll questions. User has to answer required polls. Some polls accepts multiple answers and some needs one answer. For single answer, I am using radiobutton list and for multiple, checkbox list is used. I want to show error message while user doesn't answer required polls. If checkbox list alone is there, validation is working correctly. If radiobutton list is there, while clicking submit, post back occurs and the answered polls get submitted even required polls not answered. Can anyone please help me to solve this?

For Checkbox list validation, I have used the code present in the following link:

http://stackoverflow.com/questions/5883273/validation-for-checkboxlist

Here is my design:
<div id="pollQuestion_div"  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>
            <Custom:RFVCBoxList ID="RFVCBoxList" runat="server" ControlToValidate="CheckBoxListMultiple"  ErrorMessage="*" Display="Dynamic" ForeColor="Red" SetFocusOnError="True"></Custom:RFVCBoxList>
            <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="CheckBoxListMultipleRequiredPoll" runat="server" RepeatDirection="Vertical" RepeatColumns="1" ></asp:CheckBoxList>         
            <asp:CheckBoxList ID="CheckBoxListMultipleNonRequiredPoll" runat="server" RepeatDirection="Vertical" RepeatColumns="1" ></asp:CheckBoxList>
          </ItemTemplate>
      </asp:DataList>  
      </div>
     
        <asp:Button ID="btnSubmitPoll" runat="server"  Text="Submit" CssClass="custom-button" OnClick="btnSubmitPoll_click" />  
Code Behind:
protected void PollDataList_ItemDataBound(object sender, DataListItemEventArgs e)
      {
       
        try
        {
          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();
            if (strPollType == "RadioButtonList")
            {
              if (strPollRequiredorNot == "Required")
              {
                RadioButtonList rbReq = (RadioButtonList)e.Item.FindControl("rdblstRequiredPollOptions");
                rbReq.ValidationGroup = "ReqFv";
                foreach (string opt in pollOptions.Split('}'))
                {
                  rbReq.Items.Add(opt.ToString());
                }
                RequiredFieldValidator rfvRadioButtonList = (RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidatorReqPoll");
                rfvRadioButtonList.ControlToValidate = rbReq.ID;
                rfvRadioButtonList.Enabled = true;
                var chkReq  = (Custom.Validators.RFVCBoxList)e.Item.FindControl("RFVCBoxList");
                chkReq.Enabled = false;
              }
              else
              {
                RadioButtonList rbNonReq = (RadioButtonList)e.Item.FindControl("rdblstNonRequiredPollOptions");
                foreach (string opt in pollOptions.Split('}'))
                {
                  rbNonReq.Items.Add(opt.ToString());
                }
              
                var validator = (RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidatorReqPoll");
                validator.Enabled = false;
                var chkReq = (Custom.Validators.RFVCBoxList)e.Item.FindControl("RFVCBoxList");
                chkReq.Enabled = false;
              }
            }
            else if (strPollType == "CheckBoxList")
            {
              if (strPollRequiredorNot == "Required")
              {
                CheckBoxList chkList = (CheckBoxList)e.Item.FindControl("CheckBoxListMultipleRequiredPoll");
                chkList.RepeatDirection = RepeatDirection.Vertical;
                chkList.RepeatLayout = RepeatLayout.Table;
                chkList.RepeatColumns = 1;
                foreach (string opt in pollOptions.Split('}'))
                {
                  chkList.Items.Add(opt.ToString());
                }
                Custom.Validators.RFVCBoxList chkReq = (Custom.Validators.RFVCBoxList)e.Item.FindControl("RFVCBoxList");
                chkReq.ControlToValidate = chkList.ID;
                chkReq.Enabled = true;
               
                var validator = (RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidatorReqPoll");
                validator.Enabled = false;
              }
              else
              {
                CheckBoxList chkList = (CheckBoxList)e.Item.FindControl("CheckBoxListMultipleNonRequiredPoll");
                chkList.RepeatDirection = RepeatDirection.Vertical;
                chkList.RepeatLayout = RepeatLayout.Table;
                chkList.RepeatColumns = 1;
                foreach (string opt in pollOptions.Split('}'))
                {
                  chkList.Items.Add(opt.ToString());
                }
              
                var validator = (RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidatorReqPoll");
                validator.Enabled = false;
                var chkReq = (Custom.Validators.RFVCBoxList)e.Item.FindControl("RFVCBoxList");
                chkReq.Enabled = false;
              }
            }
          }
        }
        catch (Exception ex)
        {
          clsLog.Instance.Write(ex.ToString());
        }
      }
Anandh Ramanujam replied to Sudha Duraisamy on 18-Sep-12 10:09 AM
set ValidationGroup for the required controls and submit button...

..and also, check Page.IsValid is true, while submit...