This article will show you how you can bind and validate the
gridview textbox using jQuery in asp.net using c#.net
Some of my previous articles are as follows: Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#, Asp.Net
Login Form With User Role Selection By Dropdownlist In Asp.net Using C#.Net,
Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Random
Character and String Generation Using C#.Net, Windows Application and Linq ,
Display
Calendar Control Selected Date Into Javascript Alert Message In Asp.Net.
So for his article first we will create a new asp.net
application and add the below code into the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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>Bind and
Validate GridView TextBox Value by jQuery In Asp.Net Using C# </title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
function
ValidateTextBox() {
var
textvalue = $(".textClass").attr('value');
if
(textvalue == "") {
alert("Please enter value in all textboxes.");
return
false;
}
return
true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" Width="89px"
OnClientClick="javascript:return ValidateTextBox();" />
</td>
</tr>
<tr>
<td align="center">
<asp:Label ID="lblmessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Address"
HeaderText="Address"
/>
<asp:BoundField DataField="UserType"
HeaderText="UserType"
/>
<asp:TemplateField>
<HeaderTemplate>
Percentage
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
CssClass="textClass"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
Just check the above code in this cod first we will check the
grid view text box. In this textbox I have assigned a dummy css class, you can
also use the class. This css class we will use for validating the textbox
value.
Now check the jquery function.
<title>Bind and Validate GridView
TextBox Value by jQuery In Asp.Net Using C# </title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
function
ValidateTextBox() {
var
textvalue = $(".textClass").attr('value');
if
(textvalue == "") {
alert("Please enter value in all textboxes.");
return
false;
}
return
true;
}
</script>
|
In above code I have added the jquery library reference and with
the help of jquery I have get the textbox value. After getting value I have
checked weather the text control is having any value or not. If it’s not having
value we will get the alert message and if it’s having value then post back
will take place.
Now we have done run the application and check the output.
0 comments:
Please let me know your view