This is a migrated thread and some comments may be shown as answers.

Add custom validation for RadComboBox with Checkboxes

2 Answers 177 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Rajesh
Top achievements
Rank 1
Rajesh asked on 13 Nov 2013, 02:10 PM
Hi 

I want to add validation for Radcombobox which contains checkboxes for multiple selection. The issue is the custom validation handler is getting fired on page load. I want to get if fired every time when the user clicks on submit.

Please find the code below ;

<telerik:RadComboBox ID="ddlAcc"  runat="server" DataValueField="Value" DataTextField="Text" Width="270" AllowCustomText="true" ExpandDirection="Up"   AutoPostBack="true" Height="150" CausesValidation="true" HighlightTemplatedItems="true" OnItemDataBound="ddlAcc_ItemDataBound" OnSelectedIndexChanged="ddlAcc_SelectedIndexChanged">
                    <HeaderTemplate>
                      <asp:CheckBox runat="server"  ID="SelectAll" />
                      <asp:Label runat="server" ID="hdrLabel" Font-Bold="false" AssociatedControlID="SelectAll">Select All</asp:Label>
                  </HeaderTemplate>
                  <ItemTemplate>
                      <div onclick="StopPropagation(event)">
                          <asp:CheckBox runat="server" ID="chk1" />
                          <asp:Label runat="server" ID="label1" Font-Bold="false" Font-Size="Smaller" AssociatedControlID="chk1"><%# DataBinder.Eval(Container, "Text") %></asp:Label>
                      </div>
                  </ItemTemplate>
              </telerik:RadComboBox>   
               <asp:CustomValidator ID="cvAccNos" runat="server" ErrorMessage="<%$ Resources: ErrorMessages, error.missing.accountnumber %>"
                      OnServerValidate="cvAccNos_ServerValidate" ControlToValidate="ddlAcc"
                      Text="<%$ Resources: ErrorMessages, error.validatoricon %>"  EnableClientScript="False" />

In the code behind :

protected void cvAccNos_ServerValidate(object sender, ServerValidateEventArgs args)
       {
           args.IsValid = !string.IsNullOrEmpty(GetCheckBoxValues());
       }

Please help . This is urgent

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Nov 2013, 10:13 AM
Hi Rajesh,

Please have a look into the sample code snippet to call a server side event of a CustomValidator on ButtonClick.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1"
    DataValueField="id" EmptyMessage="Select" DataTextField="text" AutoPostBack="true">
</telerik:RadComboBox>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server"
    ErrorMessage="*" OnServerValidate="CustomValidator1_ServerValidate" ControlToValidate="RadComboBox1"
    Text="*" />
<br />
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Submit" CausesValidation="true">
</telerik:RadButton>

C#:
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs args)
{
    string selectedtext = RadComboBox1.Text;
    if (selectedtext == "")
    {
        args.IsValid = false;
        Response.Write("<script>alert('Please select an Item');</script>");
    }
    else
    {
        args.IsValid = true;
        Response.Write("<script>alert('Success');</script>");
    }
}

Thanks,
Shinu.
0
sharon
Top achievements
Rank 1
answered on 24 Apr 2017, 07:16 AM

Hi Shinu,

   I want to check validation for RadComboBox and use the above solution(CustomValidator1_ServerValidate), it works but at the case of (selectedtext !="" and args.IsValid = true), the loop repeated for 3 times.  What's the reason for the repeatition?

Thanks in advance!

Sharon

 

Tags
ComboBox
Asked by
Rajesh
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
sharon
Top achievements
Rank 1
Share this question
or