【发布时间】:2018-03-10 03:29:35
【问题描述】:
我在 gridview 中动态创建了一个列,以便在每一行中显示一个按钮。并且正在尝试让 onclick 事件起作用。
ButtonField test = new ButtonField();
test.Text = "Details";
test.ButtonType = ButtonType.Button;
test.CommandName = "test";
GridView1.Columns.Add(test);
我的 asp 基础,因为一切都是动态添加到 gridview 中的:
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
这很好地附加了按钮,但是我似乎找不到在测试按钮字段上添加点击事件的参数。
我试过了:
void viewDetails_Command(Object sender, GridViewRowEventArgs e)
{
if (test.CommandName == "test")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Event works')", true);
}
}
这不会运行,因为我认为它没有绑定到任何东西,但是我看不到我将把这个事件函数绑定到哪里?只需使用警报消息来测试 onclick 的工作原理!
任何帮助都会很棒!
【问题讨论】:
-
你什么时候创建那个按钮?
-
ButtonField 正在范围顶部启动,按钮在页面加载时添加到列中。
标签: c# asp.net gridview event-handling