From: Peter Afonin on
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


From: Brian Cryer on
"Peter Afonin" <pafonin(a)hotmail.com> wrote in message
news:el1HkRuTLHA.2100(a)TK2MSFTNGP04.phx.gbl...
> 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

From: Peter Afonin on
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(a)localhost> wrote in message
news:u6oMpD3TLHA.5944(a)TK2MSFTNGP06.phx.gbl...
> "Peter Afonin" <pafonin(a)hotmail.com> wrote in message
> news:el1HkRuTLHA.2100(a)TK2MSFTNGP04.phx.gbl...
>> 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
>


From: Brian Cryer on
"Peter Afonin" <pafonin(a)hotmail.com> wrote in message
news:OokJUR3TLHA.2100(a)TK2MSFTNGP04.phx.gbl...
> 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

From: Peter Afonin on
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(a)localhost> wrote in message
news:%23SKUUTAULHA.4344(a)TK2MSFTNGP02.phx.gbl...
> "Peter Afonin" <pafonin(a)hotmail.com> wrote in message
> news:OokJUR3TLHA.2100(a)TK2MSFTNGP04.phx.gbl...
>> 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
>