【发布时间】: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 填充的,那么点击事件如何知道点击了哪一行?
【问题讨论】: