How to get cell value in a GridView on LinkButton Click Event? Or RowCommand?

how to get the gridview values for the selected row on link button click
get gridview cell value on button click
get gridview textbox value in c# on button click
how to pass value from linkbutton in gridview in asp net
how to get the gridview values for the selected row on button click
find linkbutton control in gridview
asp net c# gridview rowcommand linkbutton
how to change link button text in gridview

I Have a GridView which conntains multiple records and couple of Link Buttons Named Edit and Detail. Now i want to Get the Name of the User (NOT Index), when a user click on Detail Link Button. Like "Name", "FatherName" etc Here is the .aspx code

<asp:GridView ID="dgvEmployeesInformation" runat="server" CssClass=" table table-bordered table-hover table-responsive" DataKeyNames="Id" AutoGenerateColumns="False" OnRowCommand="dgvEmployeesInformation_RowCommand" OnRowDataBound="dgvEmployeesInformation_RowDataBound" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="dgvEmployeesInformation_PageIndexChanging">
    <%--1st Column--%>
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="Id" ControlStyle-BackColor="#0066ff" Visible="False">
            <ControlStyle BackColor="#0066FF"></ControlStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:BoundField HeaderText="Employee No" DataField="EmployeeNo" />
        <asp:BoundField HeaderText="Father Name" DataField="FatherName" />
        <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="AddEmployeeBasic1.aspx?thid={0}" HeaderText="Update" NavigateUrl="~/AddEmployeeBasic1.aspx" Text="Edit" />
        <asp:TemplateField HeaderText="Action" ShowHeader="True">
            <ItemTemplate>
                <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lbDetail" OnClick="lbDetail_Click" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="EmployeesDetails.aspx?EmpID={0}" NavigateUrl="~/EmployeesDetails.aspx" HeaderText="Show Detail" Text="Detail"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Here is the lbDetail_Click Code

protected void lbDetail_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
    Label lblUserName = (Label)clickedRow.FindControl("Name");
    EmployeeID.EmpName = lblUserName.ToString();
}

When i put my program on Debugging mode, the lblUserName return NULL Here is the picture.

What i want is that, when a user click on Detail LinkButton, then on lbDetail Click event, it gets the Name of the Employee and store it in a static variable. Following is the picture

I don't understand how to solve this problem. Please help me through this. Your help will be really appreciated.

I would just add data attributes to the details button then get it's values at code behind.

For example:

1.) Add new data-myData='<%# Eval("Name") %>' attribute and its value to button

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton runat="server" ID="lbDetail" OnClick="lbDetail_Click" Text="Detail" data-ID='<%# Eval("ID") %>' data-myData='<%# Eval("Name") %>' ></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

2.) Get those data from event handler

protected void lbDetail_Click(object sender, EventArgs e)
{
     LinkButton button = (LinkButton)sender;
     var name = (string)button.Attributes["data-myData"];
     var selectedID = (string)button.Attributes["data-ID"];
     Session["selectedID"] = selectedID ;
}

Get GridView Cell value on LinkButton Click in ASP.Net using C# , Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) If e.CommandName = "Select" Then. 'Determine the RowIndex of the Row whose LinkButton was clicked. Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument) 'Reference the GridView Row. In this article I will explain with an example, how to get cell value of GridView Row in RowCommand and RowDataBound events in ASP.Net using C# and VB.Net. HTML Markup The following HTML Markup consists of an ASP.Net GridView three BoundField columns and a ButtonField column.

lblUserName is null because it's not a Label, but a BoundField. What you could do it get the Cell value.

protected void lbDetail_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
    Label1.Text = clickedRow.Cells[1].Text;
}

Or use a TemplateField that does contain a Label Name

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

how to get a cell value from gridview ,on link button inside gridview,c , i have 3 column in a gridview i.e 2 label and a link button on my link button click of specific row,i want the value of label1. my link button is in 3rd column pf my in the linkbutton and in the rowcommand event have to check. I want that on click of the link button the value of first column in that row of gridview becomes the text for the textbox testtb. Somehow,the value for name remains null. EDIT

Here is how your code should look:

protected void lbDetail_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
    var username = clickedRow.Cells[1].Text;
    if(string.IsNullOrEmpty(username))
    {
        return;
    }
    EmployeeID.EmpName = username;
}

how to selected row in GridView on LinkButton Click event?, When i click this button, i need to the value of first cell of the selected row. and click to generate postback event then GridView RowCommand Event will fire. What i mean is that when a User click on Details LinkButton, then it Navigate the user to UserDetail.aspx with the primary key Id. and then on UserDetails pageLoad Event, i retrieve the data from the Database and populate the DataList control on the basis of that Id which comes from Gridview Control.

Get row index link button gridview, The Button Click event will be implemented using the RowCommand event and hence when Duration: 9:25 Posted: Sep 3, 2018 The RowCommand Event can be used to get the selected GridView Row value or text. GridView.RowCommand Event: The RowCommand Event occurs when a button is clicked in a GridView control. < asp:GridView OnRowCommand ="GridViewCommandEventHandler" /> The RowCommand event is raised when a button is clicked in the GridView control. This enables you to

Get GridView Cell value on Button Click in ASP.Net using C# , Hi I want take value from gridview when Linkbutton from gridview cliked on link button clicked event my code is Protected Sub LinkStudentID_Click(ByV Net GridView - Get Row Index on RowCommand and Click events. Inside the Button Click event handler, first the Button is referenced using the sender object and then from the Button object the CommandArgument property is fetched. The value of the CommandArgument is split into multiples values using the Split function of String class.

How to get values from asp.net gridview cell on linkbutton click , getElementById('hdField').value = cellValue; return false; } //]]> </script> Please do let me <asp:GridView ID="grd" OnRowCommand="grd_RowCommand"> <​Columns> On link button click event write the following code. The LinkButton Click event will be implemented using the RowCommand event and hence when the LinkButton is clicked, the RowCommand event will be triggered and inside it the cell value of the ASP. net-mvc , kendo-treeview I trying to implement a cascading down list with editor template A couple days ago Piyush, a frequent reader and commenter on

Comments
  • Can you please remove that NavigateUrl from the LinkButton. What I am thinking that it is making the page/grid control unload before it hits the event.
  • Actually i need NavigateUrl Attribute. The reason is that, that i want to navigate to another Page on the basis of Primary Key Id, and load all the details of corresponding Employee.
  • Thank you, Yeah i get that, but also i want to navigate to another page with all the information of the employee on the basis of Primary Key. They way you are telling me to do, it gets the the name and store it in the static variable but don't navigate to the page that i want to navigate. It over pass the NavigateUrl attribute. Please tell me how to do that on LinkButton Click Event. Thankyou for the information btw!
  • How about setting a session variable in the click event or you can add the id in querystring:
  • Thanks buddy. I got it. That's why I am accepting your answer. #Peace
  • If you have made the Label control a bound one with an ID then I will recommend going with FindControl() method as it seems clear to me rather than tracking the column index.
  • Thanks, It gets the Name and stores it but then it not navigate to Another Page with all the Information. by Response.Redirect Method, i am able to navigate to EmployeeDetails.aspx Page but the QueryString returns NULL. Please help to resolve the issue.
  • What do you mean by not navigate to Another Page? And the same goes for empty QueryString? Maybe you want to put the value in a Session and do a Response.Redirect?
  • What i mean is that when a User click on Details LinkButton, then it Navigate the user to UserDetail.aspx with the primary key Id. and then on UserDetails pageLoad Event, i retrieve the data from the Database and populate the DataList control on the basis of that Id which comes from Gridview Control. Hope you got my query
  • Yeah i know that, but what i want is to get the name no matter what, i don't want a null value. i want a Name and store it in a static variable which i'll be using in the Details Content Page.
  • The above piece of code gives you a name. you can do whatever you want to do with it.