我已经从代码隐藏中在我的.aspx中动态创建了GridView
。我在GridView
中插入了一个sql表。然后我又添加了一个按钮归档列。这是代码:
ButtonField bf = new ButtonField();
bf.Text = "Details";
bf.ButtonType = ButtonType.Button;
DataTable table = new DataTable();
table.Load(reader);
GridView gv = new GridView();
gv.Columns.Add(bf);
gv.DataSource = table;
gv.DataBind();
现在我想在此ButtonField上添加MouseClickEvent
,但没有属性Click
或MouseClick
。有没有办法做到这一点?
答案 0 :(得分:5)
对于ButtonField
内的GridView
,请指定CommandName
:
bf.CommandName = "MyCommand";
并访问它:
void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCommand")
{
}
}
您可能会发现它很有用:ButtonField.CommandName Property。
答案 1 :(得分:2)
对于gridviews,每行都有一个动作(如编辑按钮或详细信息),我个人喜欢这样做:
OnDetailsButtonClick
)。所以这个按钮就是提交的按钮。goToDetails(entityId)
的javascript函数)
所以javascript函数看起来像:function goToDetails(entityId){
$("#HiddenEntityId").val(entityId);
$("#Button").click()
}
从后面的代码可以从隐藏字段中获取行/实体ID:
private void OnDetailsButton_Click(object sender, EventArgs e){
string entityId = HiddenEntityId.Value;
//now you can do whatever you like
}
答案 2 :(得分:0)
你必须使用'Gridview.RowCommand'句柄为ButtonField中的按钮启用自定义脚本..
即
1)将“CommandName”属性添加到buttonfield,此示例假定CommandName =“Delete”
2)
Protected Sub GridView1_buttonsclicked(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Delete" Then
'Delete clicked with index of " + e.CommandArgument.ToString
'Your code here, using the e.commandargument as the gridview index, then select column values using that index.
End If
End Sub