getting gridview row number when a template button is selected

Discussion in 'ASP .Net' started by Paul, Jul 18, 2008.

  1. Paul

    Paul Guest

    Hi I have a gridview with one of the columns as a template column with a
    button. When the user selects the button is there a way to get the row
    number in the button_Click event? Another one of the columns is a template
    column that has a label that I have displaying the row number.
    Thanks
    --
    Paul G
    Software engineer.
     
    Paul, Jul 18, 2008
    #1
    1. Advertising

  2. Paul

    Munna Guest

    Hi,

    Assign the button a commandname...
    and subscribe the rowcommand event of gridview...


    void CustomersGridView_RowCommand(Object sender,
    GridViewCommandEventArgs e)
    {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
    // Convert the row index stored in the CommandArgument
    // property to an Integer.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button clicked
    // by the user from the Rows collection.
    GridViewRow row = CustomersGridView.Rows[index];
    }

    BEST OF LUCK

    --------------
    Munna

    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
     
    Munna, Jul 18, 2008
    #2
    1. Advertising

  3. Paul

    Paul Guest

    Hi thanks for the response. I only have one button. I added the code to the
    rowcommand but the event does not fire when the button is selected, perhaps
    it is because my button is a template field.
    --
    Paul G
    Software engineer.


    "Munna" wrote:

    > Hi,
    >
    > Assign the button a commandname...
    > and subscribe the rowcommand event of gridview...
    >
    >
    > void CustomersGridView_RowCommand(Object sender,
    > GridViewCommandEventArgs e)
    > {
    > // If multiple buttons are used in a GridView control, use the
    > // CommandName property to determine which button was clicked.
    > if(e.CommandName=="Add")
    > {
    > // Convert the row index stored in the CommandArgument
    > // property to an Integer.
    > int index = Convert.ToInt32(e.CommandArgument);
    >
    > // Retrieve the row that contains the button clicked
    > // by the user from the Rows collection.
    > GridViewRow row = CustomersGridView.Rows[index];
    > }
    >
    > BEST OF LUCK
    >
    > --------------
    > Munna
    >
    > www.munna.shatkotha.com
    > www.munna.shatkotha.com/blog
    > www.shatkotha.com
    >
     
    Paul, Jul 18, 2008
    #3
  4. Paul

    Guest

    On Jul 18, 1:06 pm, Paul <> wrote:
    > Hi thanks for the response.  I only have one button.  I added the code to the
    > rowcommand but the event does not fire when the button is selected, perhaps
    > it is because my button is  a template field.
    > --
    > Paul G
    > Software engineer.
    >
    >
    >
    > "Munna" wrote:
    > > Hi,

    >
    > > Assign the button a commandname...
    > > and subscribe the rowcommand event of gridview...

    >
    > > void CustomersGridView_RowCommand(Object sender,
    > > GridViewCommandEventArgs e)
    > >   {
    > >     // If multiple buttons are used in a GridView control, use the
    > >     // CommandName property to determine which button was clicked.
    > >     if(e.CommandName=="Add")
    > >     {
    > >       // Convert the row index stored in the CommandArgument
    > >       // property to an Integer.
    > >       int index = Convert.ToInt32(e.CommandArgument);

    >
    > >       // Retrieve the row that contains the button clicked
    > >       // by the user from the Rows collection.
    > >       GridViewRow row = CustomersGridView.Rows[index];
    > > }

    >
    > > BEST OF LUCK

    >
    > > --------------
    > > Munna

    >
    > >www.munna.shatkotha.com
    > >www.munna.shatkotha.com/blog
    > >www.shatkotha.com- Hide quoted text -

    >
    > - Show quoted text -


    You have to add: onRowCommand="GridView_RowCommand" to the GridView
    asp for it to recognize the RowCommand event.

    regards,

    Big B
     
    , Jul 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil
    Replies:
    0
    Views:
    750
  2. jobs
    Replies:
    1
    Views:
    1,482
  3. Replies:
    2
    Views:
    505
    Angel
    Feb 13, 2008
  4. Want to add row under selected row of gridview

    , Feb 16, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    304
  5. Selected Row Only GridView Row Visible?

    , Oct 17, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    298
Loading...

Share This Page