C# 如何在网格视图 asp.net 中为按钮字段创建单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18802509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make click event for button field in grid view asp.net
提问by daidai
I have dynamically created GridView
in my .aspx from codebehind. I inserted a sql table in that GridView
. Then I added one more button filed column. Here is the code:
我GridView
从代码隐藏在我的 .aspx 中动态创建。我在其中插入了一个 sql 表GridView
。然后我又添加了一个按钮字段。这是代码:
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();
Now I want to add a MouseClickEvent
on this ButtonField, but there is no property Click
or MouseClick
. Is there any way to do this?
现在我想MouseClickEvent
在这个 ButtonField 上添加一个,但没有属性Click
或MouseClick
. 有没有办法做到这一点?
回答by Saad Alothman
When it comes to gridviews that each row have an action (like edit button, or details), I personally like to do the following:
当涉及到每行都有一个操作(如编辑按钮或详细信息)的 gridviews 时,我个人喜欢执行以下操作:
- Have a hidden button right after the GridView, this button has an onclick event (let's say
OnDetailsButtonClick
). So this button is the one that will be making the submission. - Create a Hidden Field that will be filled when an action from a row is clicked, so that the server side code will pick up which rowId that the action was performed on
- Make every button in the gridview to have OnClientClick (lets say the javascript function called
goToDetails(entityId)
) so the javascript function will look like:
- 在 GridView 后面有一个隐藏的按钮,这个按钮有一个 onclick 事件(比如说
OnDetailsButtonClick
)。所以这个按钮是将提交的按钮。 - 创建一个隐藏字段,当单击行中的操作时将填充该字段,以便服务器端代码将选择执行该操作的 rowId
- 使 gridview 中的每个按钮都具有 OnClientClick(让我们说调用的 javascript 函数
goToDetails(entityId)
),这样 javascript 函数将如下所示:
function goToDetails(entityId){
$("#HiddenEntityId").val(entityId);
$("#Button").click()
}
from the code behind you can get the row/Entity ID from the hidden field:
从后面的代码中,您可以从隐藏字段中获取行/实体 ID:
private void OnDetailsButton_Click(object sender, EventArgs e){
string entityId = HiddenEntityId.Value;
//now you can do whatever you like
}
回答by afzalulh
For a ButtonField
inside GridView
you specify CommandName
:
对于ButtonField
内部,GridView
您指定CommandName
:
bf.CommandName = "MyCommand";
And access it like:
并像这样访问它:
void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCommand")
{
}
}
You may find it useful: ButtonField.CommandName Property.
您可能会发现它很有用:ButtonField.CommandName 属性。
回答by Josh Harris
You have to use 'Gridview.RowCommand' handle to enable custom script for a button in a ButtonField..
您必须使用“Gridview.RowCommand”句柄为 ButtonField 中的按钮启用自定义脚本。
I.e.
IE
1) add a 'CommandName' property to your buttonfield, this example assumes the CommandName = "Delete"
1) 将“CommandName”属性添加到您的按钮字段,此示例假定 CommandName = "Delete"
2)
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