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.

 
alik13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.