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 .
But before moving forward I would like to show you some of
my previous article. File
Upload with ASP.NET | How to Use FileUpload Control in ASP.Net Using
C#.Net,VB.Net | Upload File in Asp.net and Save in Folder, Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net, Accordion
Ajax Control Toolkit Example in Asp.net OR How to Use Ajax Accordion Control in
Asp.Net.
Some of the jQuery articles are as follows Free
jQuery Spell Checker Plugins For Asp.Net, Reset
Div Height by Using jQuery, jQuery
DateTime Picker Calender In ASP.NET MVC | How To Access jQuery DateTime Picker
Calender Value in MVC Controller.
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
0 comments:
Please let me know your view