Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 单击TemplateField内的按钮时未触发RowCommand_Asp.net - Fatal编程技术网

Asp.net 单击TemplateField内的按钮时未触发RowCommand

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

基本上我有一个GridView:

        <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();
}