1

Basically I have a 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" />

And inside I have 2 TemplateFields:

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnStart" runat="server" Text="Start" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnStop" runat="server" Text="Stop" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
                    </ItemTemplate>
                </asp:TemplateField>

Finally I have the method thats supposed to fire when clicking on either of the Buttons on the gridView, problem is that when I click either of them the event does not get called at all

    public void gvServices_RowCommand(object sender, GridViewCommandEventArgs 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();
    }
4

2 に答える 2

5

GridView の RowCommand は、TemplateField 内のボタンから起動します。このスレッドへの回答を確認してください。

グリッドビュー内のリンクボタンが起動しない

于 2012-11-28T19:38:53.217 に答える
4

あなたはボタンを聞く必要がありますRowCommandGridView.RowCommandは、asp:ButtonField各行にが存在する場合に使用されます。

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>

コードビハインド:

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();
}
于 2012-08-10T14:38:43.150 に答える