Sunday 25 August 2013

Radiobutton validation using JQuery


This example for Radiobutton validation using JQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validate asp.net radiobuttonlist in JQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#btnSubmit').click(function() {
if ($('#rdbtnGender :radio:checked').length > 0) {
return true;
}
else {
alert('Please select Gender')
return false;
}
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> <b>Select Gender:</b></td>
<td>
<asp:RadioButtonList ID="rdbtnGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1" Text="Male" />
<asp:ListItem Value="2" Text="Female" />
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Checkbox validation using JQuery

This is example for Checkbox validation using JQuery


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Validate asp.net Checkboxlist in JQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#btnSubmit').click(function() {
if ($('#chktech input:checked').length > 0) {
return true;
}
else {
alert('Please select atleast one technology')
return false;
}
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> <b>Select Technologies:</b></td>
<td>
<asp:CheckBoxList ID="chktech" runat="server" >
<asp:ListItem Value="1" Text="ASP.NET" />
<asp:ListItem Value="2" Text="C#.NET" />
<asp:ListItem Value="3" Text="JQuery" />
<asp:ListItem Value="4" Text="JavaScript" />
<asp:ListItem Value="5" Text="SQL Server 2005" />
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Dropdown validation using JQuery


This is example for Dropdown validation using JQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Validate Dropdownlist in JQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#btnSubmit').click(function() {
if ($("#ddlCountry").val()>0) {
return true;
}
else {
alert('Please select Country')
return false;
}
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> <b>Select Country:</b></td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Text="Select" Value="0" />
<asp:ListItem Text="Australia" Value="1" />
<asp:ListItem Text="India" Value="2" />
<asp:ListItem Text="England" Value="3" />
<asp:ListItem Text="USA" Value="4" />
<asp:ListItem Text="China" Value="5" />
<asp:ListItem Text="Sri Lanka" Value="6" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Highlight Gridview Row When CheckBox is Checked in ASP.NET using JQuery


This example will how  we can highlight gridview row when checkbox is checked using JQuery.

ASPX PAGE

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Highlight row when checkbox is checked in gridview asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
// Header Checkbox click function
$('[id$=chkHeader]').click(function() {
if ($('[id$=chkHeader]:checked').length>0) {
$('[id$=chkChild]').parent().parent().addClass('highlightRow');
}
else {
$('[id$=chkChild]').parent().parent().removeClass('highlightRow');
}
$("[id$=chkChild]").attr('checked', this.checked);
});
// Child Checkbox click function
$("[id$=chkChild]").click(function() {
if(this.checked) {
$(this).parent().parent().addClass('highlightRow');
}
else {
$(this).parent().parent().removeClass('highlightRow');
}
if ($('[id$=chkChild]').length == $('[id$=chkChild]:checked').length) {
$('[id$=chkHeader]').attr("checked", "checked");
}
else {
$('[id$=chkHeader]').removeAttr("checked");
}
});
});
</script>
<style type="text/css">
.highlightRow
{
background-color:Red;
Color:White;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild"  runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

C# PAGE

using System;
using System.Data;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=PERSONAL\SQLEXPRESS;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select FirstName,LastName,City from UserInfo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}