Asp.net 单击TemplateField内的按钮时未触发RowCommand
基本上我有一个GridView:Asp.net 单击TemplateField内的按钮时未触发RowCommand,asp.net,Asp.net,基本上我有一个GridView: <asp:GridView ID="gvServices" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" AutoGenerateColumns="False" OnRowCommand="gvServices_RowCo
<asp:GridView ID="gvServices" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoGenerateColumns="False" OnRowCommand="gvServices_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
您需要收听按钮行命令。当每行上都有一个asp:Button字段时使用 Aspx代码:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStart" runat="server" Text="Start" OnCommand="GridButtons_Command" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Text="Stop" OnCommand="GridButtons_Command" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
您需要收听按钮行命令。当每行上都有一个asp:Button字段时使用 Aspx代码:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStart" runat="server" Text="Start" OnCommand="GridButtons_Command" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Text="Stop" OnCommand="GridButtons_Command" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
GridView上的RowCommand确实从TemplateField中的按钮触发。查看此线程的答案:
GridView上的RowCommand确实从TemplateField中的按钮触发。查看此线程的答案:
非常好的解决方案!谢谢:非常好的解决方案!谢谢:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStart" runat="server" Text="Start" OnCommand="GridButtons_Command" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Text="Stop" OnCommand="GridButtons_Command" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
public void GridButtons_Command(object sender, CommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "StartService")
{
StartServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
if (e.CommandName == "StopService")
{
StopServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
loadGridView();
}