Wednesday, 11 June 2014

RadioButtonList Validation Using jQuery in Asp.Net

6/11/2014 - By Pranav Singh 0

Radiobuttonlist control is an asp.net control. We use this control to show list of item in the form of list. In this article I will show you how you can validate a Radiobuttonlist control in asp.net Using jQuery .



Now or this article first we will create an asp.net application and add a rediobuttonlist control. After adding the control your code will look as shown below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioButtonValidation.aspx.cs" Inherits="ProjectDemo_Asp.et.RadioButtonValidation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RadioButtonList Validation Using jQuery in Asp.Net</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script language="javascript" type="text/javascript">
        function ValidateRedioButtonList() {
            if ($('#RadioButtonList1 :radio:checked').length > 0) {              
                return true;
            }
            else {
                alert('Please select attleast one value')
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
       <h3> Validate RadioButtonList </h3>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="3"
            BorderColor="#FF0066" BorderStyle="Dotted">
            <asp:ListItem>India</asp:ListItem>
            <asp:ListItem>Pakistan</asp:ListItem>
            <asp:ListItem>America</asp:ListItem>
            <asp:ListItem>Bangla Deah</asp:ListItem>
            <asp:ListItem>Afghanistan</asp:ListItem>
            <asp:ListItem>Afghanistan</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Click"
            OnClientClick="javascript:return ValidateRedioButtonList();" onclick="Button1_Click"/>
   
    </div>
    </form>
</body>
</html>

In above code below mention jQuery code is used for checking the rediobuttonlist control selection.

<script language="javascript" type="text/javascript">
        function ValidateRedioButtonList() {
            if ($('#RadioButtonList1 :radio:checked').length > 0) {              
                return true;
            }
            else {
                alert('Please select attleast one value')
                return false;
            }
        }
    </script>

For getting the selected value in our code end we have used below mention code

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class RadioButtonValidation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string countryname = RadioButtonList1.SelectedValue.ToString();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Your Selected Country", "alert('You have selected : " + countryname + ".');", true);
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class RadioButtonValidation
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim countryname As String = RadioButtonList1.SelectedValue.ToString()
            Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Your Selected Country", "alert('You have selected : " + countryname + ".');", True)
        End Sub
    End Class
End Namespace

Now we have done. View the page in browser.



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top