【问题标题】:Gridview ButtonField Click not workingGridview ButtonField 单击不起作用
【发布时间】:2016-01-22 13:31:12
【问题描述】:

所以我有一个从 SQL 存储过程填充的 gridview。一切都很好。我有一个按钮字段,我添加了一个按钮字段,单击该字段时,只会显示一个隐藏标签。我希望它做其他事情,但这是一个测试,所以我可以看看我是否得到了正确的行。

protected void ServiceList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ServiceRestart")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = ServiceList.Rows[index];
        TableCell serviceName = selectedRow.Cells[1];
        string service = serviceName.Text;

        Label1.Visible = true;
        Label1.Text = service;
    }
}

这是gridview上的代码

<asp:GridView ID="ServiceList" runat="server" AllowSorting="True" AutoGenerateColumns="False" BorderStyle="Solid" BorderWidth="2px" OnRowCommand="ServiceList_RowCommand">
    <Columns>
        <asp:BoundField DataField="ServerName" HeaderText="Server Name" />
        <asp:BoundField DataField="ServerIP" Visible="False" />
        <asp:BoundField DataField="DisplayName" HeaderText="Service Name" />
        <asp:BoundField DataField="Status" HeaderText="Status" />
        <asp:ButtonField HeaderText="Restart" Text="Restart" ButtonType="Link" CommandName="ServicResStart" />
    </Columns>
</asp:GridView>

当我点击链接时,什么都没有发生。

编辑:我猜另一个问题是,如果 gridview 是从 SQL 填充的,那么点击事件如何知道点击了哪一行?

【问题讨论】:

    标签: c# gridview


    【解决方案1】:

    所以如果有人遇到同样的问题,我就可以解决这个问题。基本上将这两行合并成新的 serviceName 行。

    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = ServiceList.Rows[index];
    var serviceName = selectedRow.Cells[1].Text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2020-12-10
      相关资源
      最近更新 更多