How to get the button ID in GridView

Discussion in 'ASP .Net' started by Peter Afonin, Sep 8, 2010.

  1. Peter Afonin

    Peter Afonin Guest

    Hello,

    My GridView has several buttons in defferent columns. When the button is
    clicked, is there a way to get a clicked button ID in the
    GridView_RowCommand event?

    Thank you,

    Peter
    Peter Afonin, Sep 8, 2010
    #1
    1. Advertising

  2. Peter Afonin

    Peter Afonin Guest

    Thanks, Bryan.

    This is one of my buttons:

    <asp:ImageButton ID="ibDestroyC" runat="server"
    ImageUrl="~/img/destroy2.PNG"
    CommandName="Destroy" CommandArgument='<%# Container.DataItemIndex %>' />

    On RowCommand I'm using the CommandArgument to get the row index,
    CommandName to indicate the command that will be executed.

    What I'm trying to get an ImageButton ID - "ibDestroyC".

    Alternatively, if I could get the current column index - this would work for
    me as well.

    Peter

    "Brian Cryer" <not.here@localhost> wrote in message
    news:...
    > "Peter Afonin" <> wrote in message
    > news:...
    >> Hello,
    >>
    >> My GridView has several buttons in defferent columns. When the button is
    >> clicked, is there a way to get a clicked button ID in the
    >> GridView_RowCommand event?

    >
    > When GridView_RowCommand is called the second parameter "e" (of type
    > System.Web.UI.WebControls.GridViewCommandEventArgs) has a property called
    > CommandName. Your buttons have a corresponding CommandName property.
    > Unless there is a way round it that I don't know, all the buttons in a
    > given column will share the same CommandName, so this is sufficient to get
    > you the button/column but not which row. Looking at my code
    > e.CommandArgument seems to contain the row, but I don't know whether
    > that's automatic or whether I had to do anything to get it there.
    >
    > Hope this helps.
    > --
    > Brian Cryer
    > http://www.cryer.co.uk/brian
    >
    Peter Afonin, Sep 8, 2010
    #2
    1. Advertising

  3. Peter Afonin

    Peter Afonin Guest

    Brian,

    Yes, I have 5 buttons with the same command name in one gridview, that's why
    I want to get either the button name or the column index.

    Of course there are alternative solutions - I can assign the different
    command names, or use the Click event for each button, like this:

    protected void ibCreateK_Click(object sender, ImageClickEventArgs e)
    {
    GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
    int i = gvRow.RowIndex;
    Label lblAction = (Label)gvRow.FindControl("lblKAction");
    Label lblPlateNumber = (Label)gvRow.FindControl("lblInset2");
    CreateDestroy(lblPlateNumber.Text, lblAction.Text, "K");

    }

    where CreateDestroy is an actual routine that needs to be executed to enter
    the data into the database.

    This will work, but it's pretty bulky and requires a lot of duplicated code
    (I have 5 buttons with one CommandName ("Create") and 5 with another
    ("Destroy"). If I could extract the button name or get the column index I
    could combine all this in one compact routine in the RowCommand event.

    Thanks,

    Peter

    "Brian Cryer" <not.here@localhost> wrote in message
    news:%...
    > "Peter Afonin" <> wrote in message
    > news:...
    >> Thanks, Bryan.
    >>
    >> This is one of my buttons:
    >>
    >> <asp:ImageButton ID="ibDestroyC" runat="server"
    >> ImageUrl="~/img/destroy2.PNG"
    >> CommandName="Destroy" CommandArgument='<%# Container.DataItemIndex %>' />
    >>
    >> On RowCommand I'm using the CommandArgument to get the row index,
    >> CommandName to indicate the command that will be executed.
    >>
    >> What I'm trying to get an ImageButton ID - "ibDestroyC".
    >>
    >> Alternatively, if I could get the current column index - this would work
    >> for me as well.

    >
    > Well, if CommandName gives you the name of the command and CommandArgument
    > the row index, then isn't that sufficient? or do you have more than one
    > button with the same command name?
    >
    > Looking at some of my output, I'm not seeing the button ID in the HTML, so
    > I'm not sure that you can get at "ibDestroyC". The commandname is the
    > closest thing I'm aware of.
    >
    > Why do you want to get the ID of the image button? I'm only asking incase
    > there is some other way of achieving what you want.
    > --
    > Brian Cryer
    > http://www.cryer.co.uk/brian
    >
    Peter Afonin, Sep 9, 2010
    #3
  4. Peter Afonin

    Peter Afonin Guest

    Yes, that's probably what I'll do.

    I'm just surprised a little that there is no easy way to get the the button
    name or column index, but I couldn't find anything either.

    Thank you,

    Peter

    "Brian Cryer" <not.here@localhost> wrote in message
    news:%23yG%...
    > "Peter Afonin" <> wrote in message
    > news:...
    >> Brian,
    >>
    >> Yes, I have 5 buttons with the same command name in one gridview, that's
    >> why I want to get either the button name or the column index.
    >>
    >> Of course there are alternative solutions - I can assign the different
    >> command names, or use the Click event for each button, like this:
    >>
    >> protected void ibCreateK_Click(object sender, ImageClickEventArgs e)
    >> {
    >> GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
    >> int i = gvRow.RowIndex;
    >> Label lblAction = (Label)gvRow.FindControl("lblKAction");
    >> Label lblPlateNumber = (Label)gvRow.FindControl("lblInset2");
    >> CreateDestroy(lblPlateNumber.Text, lblAction.Text, "K");
    >>
    >> }
    >>
    >> where CreateDestroy is an actual routine that needs to be executed to
    >> enter the data into the database.
    >>
    >> This will work, but it's pretty bulky and requires a lot of duplicated
    >> code (I have 5 buttons with one CommandName ("Create") and 5 with another
    >> ("Destroy"). If I could extract the button name or get the column index I
    >> could combine all this in one compact routine in the RowCommand event.

    >
    > I'd forgotten about the FindControl solution.
    >
    > I can't think of anything else other than the two approaches you've
    > mentioned (Click & FindControl or different CommandNames).
    >
    > I don't think your Click event solution is that bulky, not if you move it
    > into a separate subroutine and in your Click event handler call it with
    > the names of the control(s) you are after. That way you have 5 one line
    > functions plus 1 which does all the real work.
    > --
    > Brian Cryer
    > http://www.cryer.co.uk/brian
    >
    Peter Afonin, Sep 9, 2010
    #4
  5. "Peter Afonin" <> skrev i meddelelsen
    news:...
    > Hello,
    >
    > My GridView has several buttons in defferent columns. When the button is
    > clicked, is there a way to get a clicked button ID in the
    > GridView_RowCommand event?
    >
    > Thank you,
    >
    > Peter
    >


    Hi...

    Can you use this.. It returns the controlID for the control, that caused the
    postback..

    private string getPostBackControlName()
    {
    Control control = null;
    //first we will check the "__EVENTTARGET" because if post back made
    by the controls
    //which used "_doPostBack" function also available in Request.Form
    collection.
    string ctrlname = Page.Request.Params["__EVENTTARGET"];
    if (ctrlname != null && ctrlname != String.Empty)
    {
    control = Page.FindControl(ctrlname);
    }
    // if __EVENTTARGET is null, the control is a button type and we
    need to
    // iterate over the form collection to find it
    else
    {
    string ctrlStr = String.Empty;
    Control c = null;
    foreach (string ctl in Page.Request.Form)
    {
    //handle ImageButton they having an additional
    "quasi-property" in their Id which identifies
    //mouse x and y coordinates
    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
    {
    ctrlStr = ctl.Substring(0, ctl.Length - 2);
    c = Page.FindControl(ctrlStr);
    }
    else
    {
    c = Page.FindControl(ctl);
    }
    if (c is System.Web.UI.WebControls.Button ||
    c is System.Web.UI.WebControls.ImageButton)
    {
    control = c;
    break;
    }
    }
    }
    return control.ID;

    }

    /Finn
    Finn Stampe Mikkelsen, Sep 9, 2010
    #5
    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. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,959
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  2. Fredrik Lundh
    Replies:
    0
    Views:
    381
    Fredrik Lundh
    Jul 6, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    377
    Steve Holden
    Jul 14, 2006
  4. Kat
    Replies:
    1
    Views:
    559
  5. albert kao
    Replies:
    1
    Views:
    2,014
    marisa lee
    Apr 10, 2010
Loading...

Share This Page