Tuesday, November 4, 2014

How to Get (Access) Gridview Footer Controls id in JavaScript

Introduction

Here I will explain how to access or get 
asp.net gridview footer control ids or values in JavaScript or find controls inside gridview in JavaScript or find asp.net gridview footer controls (textbox, dropdownlist, checkbox, radio button etc..) values or ids in JavaScript.

Description: 

In previous posts I explained 
bind data to textbox control in asp.net gridview, Cascading Dropdownlist in inside of asp.net gridview, populate one dropdown based on another dropdown in asp.net, bind data to dropdownlist in asp.net gridview and many articles relating to gridview, asp.net, c#. Now I will explain how to get asp.net gridview footer control ids or values in JavaScript.

To get asp.net gridview footer control ids or values in JavaScript we need to write the code like as shown below

Syntax to Get Gridview Footer Controls in JavaScript


<script type="text/javascript">
function GetGridFooterRowvalues() {
var fuid = document.getElementById('<%=((TextBox)gvUserInfo.FooterRow.FindControl("txtUserId")).ClientID %>');
if (fuid != null && funame != null && fueducation != null) {
alert('UserId:' + fuid.value)
}
}
</script>
If you want see it in complete example open your aspx page and write the following code


<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 fuid = document.getElementById('<%=((TextBox)gvUserInfo.FooterRow.FindControl("txtUserId")).ClientID %>');
var funame = document.getElementById('<%=((TextBox)gvUserInfo.FooterRow.FindControl("txtUserName")).ClientID %>');
var fueducation = document.getElementById('<%=((TextBox)gvUserInfo.FooterRow.FindControl("txtEducation")).ClientID %>');
if (fuid != null && funame != null && fueducation != null) {
alert('UserId:' + fuid.value + ';UserName:' + funame.value + ';Education:' + fueducation.value)
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="UserId">
<ItemTemplate>
<asp:Label id="lblUserid" runat="server"  Text='<%# Eval("UserId") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUserId" runat="server" Text="100" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label id="lblUsername" runat="server" Text='<%# Eval("UserName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text="SureshD" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Education">
<ItemTemplate>
<asp:Label id="lblEducation" runat="server" Text='<%# Eval("Education") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEducation" runat="server" Text="B.Tech" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGet" Text="Get Footer Values" runat="server"OnClientClick="GetGridFooterRowvalues()" />
</div>
</form>
</body>
</html>
After that write the following code in code behind

C# Code


using System;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserId", typeof(Int32));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("Education", typeof(string));
            dt.Rows.Add(1, "Suresh Dasari", "B.Tech");
            dt.Rows.Add(2, "Rohini Dasari", "Msc");
            dt.Rows.Add(3, "Madhav Sai", "MS");
            dt.Rows.Add(4, "Praveen", "B.Tech");
            dt.Rows.Add(6, "Sateesh", "MD");
            dt.Rows.Add(7, "Mahesh Dasari", "B.Tech");
            dt.Rows.Add(8, "Mahendra", "CA");
            gvUserInfo.DataSource = dt;
            gvUserInfo.DataBind();
        }
    }
}
VB.NET Code


Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As New DataTable()
            dt.Columns.Add("UserId", GetType(Int32))
            dt.Columns.Add("UserName", GetType(String))
            dt.Columns.Add("Education", GetType(String))
            dt.Rows.Add(1, "Suresh Dasari", "B.Tech")
            dt.Rows.Add(2, "Rohini Dasari", "Msc")
            dt.Rows.Add(3, "Madhav Sai", "MS")
            dt.Rows.Add(4, "Praveen", "B.Tech")
            dt.Rows.Add(6, "Sateesh", "MD")
            dt.Rows.Add(7, "Mahesh Dasari", "B.Tech")
            dt.Rows.Add(8, "Mahendra", "CA")
            gvUserInfo.DataSource = dt
            gvUserInfo.DataBind()
        End If
    End Sub
End Class
Demo

 How to Get (Access) Gridview Footer Controls id in JavaScript

No comments:

Post a Comment