Thursday 16 May 2013

Code to Find Control Value on GridView RowCommand Event in Asp.Net Using C#

Code to Find Control Value on GridView RowCommand Event in Asp.Net Using C#

o your page code will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1"
    EnableEventValidation="false" %>

<!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>How to edit and update image in gridview Using Asp.net in C#
    </title>   
</head>
<body>
    <form id="form1" runat="server">
    <div>   
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
            Width="400px" CellPadding="3"
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
            BorderWidth="1px" onrowcommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text=`<%# Bind("Name") %>`></asp:Label>
                    </ItemTemplate>               
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblcity" runat="server" Text=`<%# Bind("City") %>`></asp:Label>
                    </ItemTemplate>               
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lbladdress" runat="server" Text=`<%# Bind("Address") %>`></asp:Label>
                    </ItemTemplate>               
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="SelectRowValue"
                            style="font-weight: 700">Get Value</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />  
        </asp:GridView>
        <br />
        <br />
        <div runat="server" id="divmessage"></div>
        &nbsp;</div>
    </form>
</body>
</html>


Now in .cs page add the below code.
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;
using System.IO;

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection _objcon = new SqlConnection("Data Source=VINAY-809553B1F\\TSS;uid=so;pwd=123;Initial Catalog=DEMO;Integrated Security=True;");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BingGridViewData();
            }
        }
        //code to bind grid view
        protected void BingGridViewData()
        {
            DataTable dt = new DataTable();
            string query = "SELECT * FROM Student";
            SqlDataAdapter _objda = new SqlDataAdapter(query, _objcon);
            _objda.SelectCommand.CommandType = CommandType.Text;
            _objcon.Open();
            _objda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            _objcon.Close();
        }
      
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "SelectRowValue")
            {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                Label lblname = (Label)row.Cells[0].FindControl("lblname");
                Label lblcity = (Label)row.Cells[0].FindControl("lblcity");
                Label lbladdress = (Label)row.Cells[0].FindControl("lbladdress");
                divmessage.InnerHtml = "Name :" + lblname.Text + "<br/>City :" + lblcity.Text + "<br/>Address :" + lbladdress.Text;
            }
        }
    }
}


Now run the application

gridview

Click on link

your break point will hit

gridview

here is the final string.

gridview

Final output.

gridview

________________________________

DOWNLOAD

1 comment: