Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 16 April 2015

Introduction: 

Some time while working in ASP.NET it is needed to find out the value or access the value of controls placed in footer. This can be done by using Javascript code. 

In this article I will explain how to access or get asp.net gridview footer control ids or values in JavaScript or find asp.net gridview footer controls (textbox, dropdownlist, checkbox, radio button etc..) values or ids in JavaScript.
To get asp.net gridview footer control ids or values in JavaScript we need to write the code like as shown below

Following javascript method is used for Find gridview’s Footer Value:

<script type="text/javascript">
function GetGridFooterRowvalues() {
var fuid = document.getElementById('<%=((TextBox)gvDetails.FooterRow.FindControl("txtEmpId")).ClientID %>');
if (fid != null) {
alert('EmpId:' + fid.value)
}
}
</script>

Full Source Code For Sample Application:

Design section:

Design your page as given below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>get gridview footer control id in javascript</title>
<script type="text/javascript">
    function GetGridFooterRowvalues() {
        var fid = document.getElementById('<%=((TextBox)gvDetails.FooterRow.FindControl("txtEmpId")).ClientID %>');
        var fname = document.getElementById('<%=((TextBox)gvDetails.FooterRow.FindControl("txtName")).ClientID %>');
        var fdesig = document.getElementById('<%=((TextBox)gvDetails.FooterRow.FindControl("txtDesig")).ClientID %>');
        if (fid != null && fname != null && fdesig != null) {
            alert('EmpId:' + fid.value + ';EmployeeName:' + fname.value + ';Designation:' + fdesig.value)
        }
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<HeaderStyle BackColor="#891208" 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" Text="100" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeName">
<ItemTemplate>
<asp:Label id="lblname" runat="server" Text='<%# Eval("EmpName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" Text="SureshD" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label id="lblDesig" runat="server" Text='<%# Eval("Designation") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDesig" runat="server" Text="HR" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGet" Text="Get Footer Values" runat="server" OnClientClick="GetGridFooterRowvalues()" /></center>
</div>
</form>
</body>
</html>


Asp.Net Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class footer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvDetails.DataSource = BindDatatable();
            gvDetails.DataBind();
        }
    }
    protected DataTable BindDatatable()
    {
        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");
        return dt;
    }
}


ASP.Net Code using Vb.Net:

Imports System.Data
Imports System.Web.UI.WebControls
Imports System.IO

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
            gvDetails.DataSource = BindDatatable()
            gvDetails.DataBind()
        End If
    End Sub
    Protected Function BindDatatable() As DataTable
        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")
        Return dt
    End Function


Categories: , ,

0 comments:

Post a Comment