Get Gridview Label Value at Linkbutton Click

Get Gridview Label Value  at Linkbutton Click


Step 1: Design web page .aspx

<asp:GridView ID="gr" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="WhiteSmoke" Width="40%">
                <HeaderStyle BackColor="#3366ff" Font-Bold="true" ForeColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="City" HeaderText="City" />

                    <asp:TemplateField HeaderText="Phone">
                        <ItemTemplate>
                            <asp:Label ID="lbl_phone" runat="server" Text='<%#Eval("Phone") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Link">
                        <ItemTemplate>
                            <asp:LinkButton ID="link" runat="server" OnClick="link_Click" Text="click" ToolTip="Click To View"></asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>

 Step 2: Logic on .CS page

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Bind Gridview

            bind_gr();
        }
    }

public void bind_gr()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("City");
        dt.Columns.Add("Phone");


        dt.Rows.Add("Kandy", "Delhi", "8802***2564");
        dt.Rows.Add("Sam", "Hr", "545502*2544");
        dt.Rows.Add("Izhar", "Up", "95802*2452");
        dt.Rows.Add("Vineet", "Delhi", "88022564***");


        dt.Rows.Add("Kandy", "Delhi", "8802***2564");
        dt.Rows.Add("Sam", "Hr", "545502*2544");
        dt.Rows.Add("Izhar", "Up", "95802*2452");
        dt.Rows.Add("Vineet", "Delhi", "88022564***");

        gr.DataSource = dt;
        gr.DataBind();

    }
    protected void link_Click(object sender, EventArgs e)
    {
        LinkButton lnk = sender as LinkButton;
        GridViewRow gvr = lnk.NamingContainer as GridViewRow;
        Label lbl_p = gvr.FindControl("lbl_phone") as Label;
        Label lbl_n = gvr.FindControl("lbl_Name") as Label;
        Response.Write("Phone   :   " + lbl_n.Text+"   ");

        Response.Write("Name   :   " + lbl_p.Text);

    }


It will look like :-




Comments

Post a Comment

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click