From: Brian Cryer on 9 Sep 2010 11:50 "Peter Afonin" <pafonin(a)hotmail.com> wrote in message news:u5FkWNDULHA.2068(a)TK2MSFTNGP05.phx.gbl... > 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
From: Peter Afonin on 9 Sep 2010 12:07 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(a)localhost> wrote in message news:%23yG%23NbDULHA.5912(a)TK2MSFTNGP05.phx.gbl... > "Peter Afonin" <pafonin(a)hotmail.com> wrote in message > news:u5FkWNDULHA.2068(a)TK2MSFTNGP05.phx.gbl... >> 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 >
From: Finn Stampe Mikkelsen on 9 Sep 2010 12:16 "Peter Afonin" <pafonin(a)hotmail.com> skrev i meddelelsen 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? > > 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
First
|
Prev
|
Pages: 1 2 Prev: How to count the active ASP.NET Sessions (SQL Server)? Next: Fix for UpdatePanel |