• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1143
  • Last Modified:

How to access control from the GridView

Hello guys,

  I have several templated columns and a button control in the GridView.

On GridView_RowCommand() when user clicks on the button I need to get values from the 2 Label controls on the GridView.

How can I reach these Labels. I tried GridView1.FindControl["lblCustomerID"] but It is incorrect...

Please advice.

 
0
alik13
Asked:
alik13
  • 2
  • 2
1 Solution
 
boro_bobCommented:
Can you show us the exact code you are using when you do the GridView1.FindControl?
0
 
Sachintana DissanayakeSenior Web DeveloperCommented:
You have to first get the GridViewRow using the e.CommandSource.
Then find the control which is in the GridViewRow.

See the sample code below.

    protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ButtonCommandName")
        {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
            Label labelValue = (Label)row.FindControl("lblCustomerID");
        }
    }

Open in new window

0
 
boro_bobCommented:
That looks like it should work. When you say it is incorrect, what exactly is happening? When you debug it, step into that code. Is labelValue null?

Also, to actually get the value out of the label, you do realise you will need to add the line:

string textvalue = labelValue.Text;
0
 
alik13Author Commented:
Thank you guys for Help!

 Based on your suggestions I figured it out.
 
The code line :

 GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;

 does not work for me, because  my GridView located inside the AJAX  UpdatePanel, but

I figured out the solution & it works! So, please use this if you have your GridView inside an AJAX UpdatePanel and you need to get some values from the GridView controls in response to the OnRowCommand event when the Button is clicked. It is a good choice when you do not need to be redirected to another page (No need for QueryString)

Here the code:

protected void dgProviders_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName.Equal("ButtonCommandName"))
    {
        // *** Retreive the DataGridRow
        int row = -1;

        int.TryParse(e.CommandArgument as string, out row);

        if (row == -1)
        {
            this.ErrorDisplay.ShowError("Invalid selection...");
            return;
        }

        GridViewRow selectedRow = gvCustomers.Rows[row];

      Label lblCustomerID = (Label)row.FindControl("lblCustomerID");
      
        String customerID = lblCustomerID.Text;
     }
}
0
 
Sachintana DissanayakeSenior Web DeveloperCommented:
Good Luck!!!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now