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

Create Click EVent on Buttons in GridView

Hi,

Can someone please tell me how I can apply a click event to a button in a GridView.  I add a templateControl, and add two buttons.  What I want to do is for example, when the button is clicked , the event should be a response.write

a:
The COmmand Argument of the button
b:
The value in the 1 st Cell of that Row.

I have been casting, using different Event Args and I still cannot do it. It is really annoying.

Thanks in advance

ANdrew
0
REA_ANDREW
Asked:
REA_ANDREW
  • 4
  • 3
1 Solution
 
GavinMannionCommented:
You will need to catch the button click event in the RowCommand event.

So give your button a CommandName and then in the RowCommand event check for the CommandName and then work from there.
0
 
REA_ANDREWAuthor Commented:
Cheers Gavin, will try that now. I was hoping you would reply to this, as you are always a reliable help.

:-)

Cheers

Andrew
0
 
GavinMannionCommented:
Thanks Andrew, Unfortunately I don't have a dev environment on hand to post code but it seems right from memory at least :)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
REA_ANDREWAuthor Commented:
ok what I have so far, is this. I have placed the button in the GridView Template Control. Then I have added the onrowcommand to the gridview tag. it calls the first function and the second function is where it handles the click. What SHould I put in or am i way of the mark?

   protected void View_C(object sender, GridViewCommandEventArgs e)
    {
        Button MyButton = new Button();
        MyButton = (Button)sender;
        MyButton.Click += new EventHandler(MyButton_Click);
    }

    void MyButton_Click(object sender, EventArgs e)
    {
        throw new Exception("The method or operation is not implemented.");
       
    }
0
 
GavinMannionCommented:
Why are you creating a new event handler?

You can either just handle it in RowCommand or call another method to do the processing...

You say it hits the first function? If so then ignore the fact that it is a button and continue coding as usual.
0
 
REA_ANDREWAuthor Commented:
again, you have just EDUCATED me with two small posts. Excellent. Cheers gavin.

    protected void View_C(object sender, GridViewCommandEventArgs e)
    {
        Response.Write(e.CommandArgument+""+e.CommandName);
    }

0
 
GavinMannionCommented:
Glad to help, thanks for the kind words ....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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