Friday, 11 July 2014

GridView Asp.net Get ItemTemplate Control Value On Button Click Using C#.Net

7/11/2014 - By Pranav Singh 8

This article will explain your how you can find the control value present in an itemtemplate of a gridview when user click on button present in a gridview in asp.net using. In this article I have used asp.net, gridview, SqlConnection, SqlDataAdapter, DataTable, Lable Control.

Now in this article first we will create a new asp.net application and add a gridview control in it. After adding gridview and adding item template in it your aspx code will look as shown below. In this we have added a table with label control in which we will display the row record.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %>

<!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>GridView Asp.net Get ItemTemplate Control Value On Button Click Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
        Width="100%" BorderStyle="Solid" ShowFooter="True">
        <Columns>
            <asp:TemplateField HeaderText="Id">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("source_numb") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TITLE">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PUBLISH YEAR">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("publication_year") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("publication_year") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PRICE">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("retail_price") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("retail_price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnselect" runat="server" Text="Select" OnClick="btnselect_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#66CCFF" />
       
    </asp:GridView>
    <p>
    <table border="1">
    <tr>
    <td align="right">ID : </td>
    <td align="left"><asp:Label  ID="Label8"  runat="server" Text=""></asp:Label></td>
    </tr>
     <tr>
    <td align="right">TITLE : </td>
    <td align="left"><asp:Label ID="Label5" runat="server" Text=""></asp:Label></td>
    </tr>
     <tr>
    <td align="right">PUBLISH YEAR : </td>
    <td align="left"><asp:Label ID="Label6" runat="server" Text=""></asp:Label></td>
    </tr>
     <tr>
    <td align="right">PRICE : </td>
    <td align="left"><asp:Label ID="Label7" runat="server" Text=""></asp:Label></td>
    </tr>
    </table>
    </p>
    </form>
</body>
</html>

Now we will generate the button click event to detect the row control value in gridview. Here is the code which you have to put on button click event which is present in gridview itmtemplate.

/// <summary>
        /// This function is used for getting control value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnselect_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = ((Button)sender).Parent.Parent as GridViewRow;
            Label lblid = (Label)gvr.FindControl("Label1");
            Label lbltitle = (Label)gvr.FindControl("Label2");
            Label lblpublishyear = (Label)gvr.FindControl("Label3");
            Label lblprice = (Label)gvr.FindControl("Label4");

            Label8.Text = lblid.Text;
            Label5.Text = lbltitle.Text;
            Label6.Text = lblpublishyear.Text;
            Label7.Text = lblprice.Text;
        }

Now please check the complete code to bind and detect the control value on button click.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        public string connectionstring = "<-----SQL data base connection------->";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase();
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
            }
        }

        /// <summary>
        /// Function for binding retribing the data from database
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from Books;";
            SqlConnection _objcon = new SqlConnection(connectionstring);
            SqlDataAdapter _objda = new SqlDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
        /// <summary>
        /// This function is used for getting control value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnselect_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = ((Button)sender).Parent.Parent as GridViewRow;
            Label lblid = (Label)gvr.FindControl("Label1");
            Label lbltitle = (Label)gvr.FindControl("Label2");
            Label lblpublishyear = (Label)gvr.FindControl("Label3");
            Label lblprice = (Label)gvr.FindControl("Label4");

            Label8.Text = lblid.Text;
            Label5.Text = lbltitle.Text;
            Label6.Text = lblpublishyear.Text;
            Label7.Text = lblprice.Text;
        }

    }
}

Now run the application by putting break point on button click event. Your screen will look as shown below.



Now click on button of a row. Your break point will hit and your will see that you are able to get the value of the controls which are present in the itemtemplate of the gridview.


Now pres F5 and check the final output

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

8 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top