Introduction:
In this article I will explain how to validate textboxes
placed inside the gridview control. We can validate control by using server
side code, but it will slow down the performance. So we will use javascript to
validate controls (textbox, dropdownlist, checkbox, radio button etc..) placed inside gridview.
Method to Get or Validate
Gridview Controls in JavaScript :
<script type="text/javascript">
function GetGridFooterRowvalues() {
var fid = document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtEmpId")).ClientID
%>');
var fname
= document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtEmpName")).ClientID
%>');
var fDesig = document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtDesig")).ClientID
%>');
if (fid.value != ''
&& fname.value != '' &&
fDesig.value != '') {
alert('EmpId:' + fid.value + ';EmpName:' + fname.value + ';Designation:' + fDesig.value)
}
else {
alert("Please enter all the values")
}
}
</script>
Full Source Code: Sample Appication
Design Section: Design page and place script as given below in
sample application.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>get or validate textbox controls inside gridview using javascript</title>
<script type="text/javascript">
function
GetGridFooterRowvalues() {
var fid = document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtEmpId")).ClientID
%>');
var fname
= document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtEmpName")).ClientID
%>');
var fDesig = document.getElementById('<%=(gvDetails.FooterRow.FindControl("txtDesig")).ClientID
%>');
if (fid.value != '' &&
fname.value != '' && fDesig.value != '') {
alert('EmpId:' + fid.value + ';EmpName:'
+ fname.value + ';Designation:' +
fDesig.value)
}
else {
alert("Please enter all the values")
}
}
</script>
</head>
<body>
<form id="form2"
runat="server">
<center>
<div>
<asp:GridView ID="gvDetails"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true">
<HeaderStyle BackColor="#A00909"
Font-Bold="true"
ForeColor="White"
/>
<Columns>
<asp:TemplateField HeaderText="EmpID">
<ItemTemplate>
<asp:Label id="lblEmpid"
runat="server"
Text='<%#Eval("EmpId") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmpId"
runat="server"
/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee
Name">
<ItemTemplate>
<asp:Label id="lblEmpname"
runat="server"
Text='<%#Eval("Empname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmpName"
runat="server"
/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label id="lblDesignation"
runat="server"
Text='<%#Eval("Designation") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDesig"
runat="server"
/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGet"
Text="Get Footer
Values" runat="server" OnClientClick="GetGridFooterRowvalues()" />
</div></center>
</form>
</body>
</html>
Asp.Net Code Behind File(using
C#)
Write
following code in code behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
public partial class
gridvalue : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected
void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpId", typeof(Int32));
dt.Columns.Add("EmpName", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Rows.Add(1, "Anil Minhas", "HR");
dt.Rows.Add(2, "Ankush rana", "Designer");
dt.Rows.Add(3, "Aman Kamboj", "Designer");
dt.Rows.Add(4, "Vijay Saklani", "Developer");
dt.Rows.Add(6, "Ajay", "Developer");
dt.Rows.Add(7, "Mayank", "Designer");
dt.Rows.Add(8, "Mannu", "QA");
gvDetails.DataSource =
dt;
gvDetails.DataBind();
}
}
Asp.Net Code Behind File(using vb.Net)
Write
following code in code behind file:
Imports System.Data
Imports System.Web.UI.WebControls
Partial Public Class
gridvalue
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs)
If Not IsPostBack Then
BindGridviewData()
End If
End
Sub
Protected
Sub BindGridviewData()
Dim dt As DataTable
= New DataTable()
dt.Columns.Add("EmpId", Type.GetType(Int32))
dt.Columns.Add("EmpName", Type.GetType(Of String))
dt.Columns.Add("Designation", Type.GetType(Of String))
dt.Rows.Add(1, "Anil Minhas", "HR")
dt.Rows.Add(2, "Ankush rana", "Designer")
dt.Rows.Add(3, "Aman Kamboj", "Designer")
dt.Rows.Add(4, "Vijay Saklani", "Developer")
dt.Rows.Add(6, "Ajay", "Developer")
dt.Rows.Add(7, "Mayank", "Designer")
dt.Rows.Add(8, "Mannu", "QA")
gvDetails.DataSource =
dt
gvDetails.DataBind()
End
Sub
End Class
0 comments:
Post a Comment