c# - In a GridView, how to add multiple commands to one item template? -
i stripped example make simple. have gridview template field. template field contains 2 buttons , label (they must in same template field). want first button set label text "win", , other button set label text "fail". onrowcommand doesnt seem triggered buttons in template field. how can accomplish this?
my gridview code below:
<asp:gridview id="gridview1" runat="server" enablemodelvalidation="true" autogeneratecolumns="false" onrowcommand="gridview1_rowcommand"> <columns> <asp:templatefield showheader="false"> <itemtemplate> <asp:button id="btnwin" runat="server" commandname="win" text="win" /> <asp:button id="btnfail" runat="server" commandname="fail" text="fail" /> <asp:label id="lblstatus" runat="server" text='<%# bind("text") %>'></asp:label> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
and code behind:
protected void page_load(object sender, eventargs e) { datatable mytable = new datatable(); mytable.columns.add("text", typeof(string)); mytable.rows.add("first entry"); mytable.rows.add("second entry"); gridview1.datasource = mytable; gridview1.databind(); } public void gridview1_rowcommand(object sender, gridviewcommandeventargs e) { //set lblstatus.text "win", or "fail" }
thanks in advance!
here go...
public void gridview1_rowcommand(object sender, gridviewcommandeventargs e) { lblstatus.text = e.commandname; }
i see there more question answered here, bear me. 1 way delegate oncommand
event of each button designated event handler, follows:
<div> <asp:gridview id="mygridview" runat="server" enablemodelvalidation="true" autogeneratecolumns="false"> <columns> <asp:templatefield showheader="false"> <itemtemplate> <asp:button id="mywinbutton" runat="server" oncommand="mywinbutton_oncommand" commandname="win" text="win" /> <asp:label id="mystatuslabel" runat="server" text='<%# bind("text") %>'/> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </div> public void mywinbutton_oncommand(object sender, commandeventargs e) { var label = ((button)sender).parent.findcontrol("mystatuslabel") label; label.text = e.commandname; }
also, alison suggests, won't see desired output of unless use !ispostback
in page_load
. furthermore, on doing in fact enable use 1 row command event handler suggested, albeit slight change in label retrieval:
public void mygridview_onrowcommand(object sender, gridviewcommandeventargs e) { var label = ((button)e.commandsource).parent.findcontrol("mystatuslabel") label; label.text = e.commandname; }
Comments
Post a Comment