Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to get the button ID in GridView

Reply
Thread Tools

How to get the button ID in GridView

 
 
Peter Afonin
Guest
Posts: n/a
 
      09-07-2010
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


 
Reply With Quote
 
 
 
 
Peter Afonin
Guest
Posts: n/a
 
      09-08-2010
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:(E-Mail Removed)...
> "Peter Afonin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>



 
Reply With Quote
 
 
 
 
Peter Afonin
Guest
Posts: n/a
 
      09-09-2010
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:%(E-Mail Removed)...
> "Peter Afonin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>



 
Reply With Quote
 
Peter Afonin
Guest
Posts: n/a
 
      09-09-2010
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%(E-Mail Removed)...
> "Peter Afonin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>



 
Reply With Quote
 
Finn Stampe Mikkelsen
Guest
Posts: n/a
 
      09-09-2010
"Peter Afonin" <(E-Mail Removed)> skrev i meddelelsen
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
get data that fills gridview inside the gridview Sergio E. ASP .Net Web Controls 0 08-08-2007 01:15 PM
How do I get rid of the select button in a gridview? Kat ASP .Net 1 04-11-2007 03:19 PM
Gridview row databound event - can't get past the 1st row of gridview maurban@gmail.com ASP .Net 5 10-13-2006 09:37 PM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM



Advertisments