C# 带有两个asp:Command 列的GridView 控件使用相同的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/946074/
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
GridView control with two asp:Command columns using the same command
提问by Michael Kniskern
I have an ASP.NET GridView
control with two asp:CommandField
columns that are both using the Select command to perform different tasks. How do I distinguish which column was selected in the OnRowCommand
event when both return "Select" when I check the CommandName
property of the GridViewCommandEventArgs
object?
我有一个GridView
带有两asp:CommandField
列的 ASP.NET控件,它们都使用 Select 命令来执行不同的任务。OnRowCommand
当我检查对象的CommandName
属性时,当两者都返回“选择”时,如何区分事件中选择了哪一列GridViewCommandEventArgs
?
Here is my source code:
这是我的源代码:
ASPX page:
ASPX 页面:
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnRowCommand="MyGridView_OnRowCommand">
<Columns>
<asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
<asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="No Click Me!" />
</Columns>
</asp:GridView>
Code behind:
后面的代码:
protected void MyGridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string x = e.CommandName //returns "Select" for both asp:CommandField columns
}
采纳答案by Mitchel Sellers
Use a button column instead that way you can specify the specific command names and work with it from there
使用按钮列,这样您就可以指定特定的命令名称并从那里使用它
<asp:ButtonField ButtonType="Link" Text="Click Me" CommandName="MyCommand1" />
<asp:ButtonField ButtonType="Link" Text="No Click Me" CommandName="MyCommand2" />
Then you can take action based on e.CommandName
然后你可以根据 e.CommandName 采取行动
回答by Cerebrus
Use the GridViewCommandEventArgs.CommandArgument property !
使用GridViewCommandEventArgs.CommandArgument 属性!
回答by CodeRedick
Well, first do you HAVE to use SELECt as the command? You could set that to something else that makes more sense.
那么,首先你必须使用 SELECT 作为命令吗?您可以将其设置为更有意义的其他内容。
Secondly, you could set the CommandArgument property to different values so you know which one is being clicked.
其次,您可以将 CommandArgument 属性设置为不同的值,以便您知道正在单击哪个值。
回答by Eric
use the command argument property.
使用命令参数属性。
e.commandargument
Or you can dynamically create the button in the command field and set the commandName to anything you want.
或者您可以在命令字段中动态创建按钮并将 commandName 设置为您想要的任何内容。
in gridview_Load
在 gridview_Load
for (int i = 0; i <= GridView1.Rows.Count - 1; i++) {
linkbutton btnedit = new linkbutton();
GridView1.Rows(i).Cells(3).Controls.Add(btnedit);
//the above is where you want the button to be on the grid
btndel.CommandName = "Select2";
btndel.CommandArgument = "whatever you want";
}
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Select1" Then
//do stuff
End Sub
回答by Eric
The answer you seek is simple and tricky. I had that problem too in my website project, so I surfed the internet for days and not found what I and you were needed.
您寻求的答案既简单又棘手。我在我的网站项目中也遇到了这个问题,所以我在网上冲浪了好几天,但没有找到我和你需要的东西。
One day I just thought about the problem alone and made experiments for hours and finally realized that the only easy way to know the column that the button was clicked is in the RowCommand event
of the GridView
in the CommandName property
of the GridViewCommandEventArgs
. More correctly probably and comfortable is to edit columns of GridView
through the design mode and replace your Command fields to Button fields.
有一天,我只是觉得这个问题单独并提出了几个小时的实验,终于意识到,只有简单的方法来知道按钮被点击的列是RowCommand event
的GridView
在CommandName property
的GridViewCommandEventArgs
。更可能和更舒适的是GridView
通过设计模式编辑列并将您的命令字段替换为按钮字段。
You can give any string/text
you want to each button field in its CommandName
, so in the RowCommand
event you can know which was clicked. Button1
or Button2
, but you don't want to give these strings, because you request something that the buttons should select and give to you, so the CommandName
should be the word select, but it can be SELECTtoo and Selectand selecTand etc.
您可以string/text
为 中的每个按钮字段提供您想要的任何内容CommandName
,以便RowCommand
您可以知道单击了哪个按钮。Button1
or Button2
,但您不想提供这些字符串,因为您请求按钮应该选择并提供给您的内容,因此CommandName
应该是单词select,但它也可以是SELECT以及Select和selecT等。
The select command can be mention in the CommandName
in many forms, and still the system will recognize it. So for example if you have two Buttonfields in the GridView
, whose first CommandName
is simply selectand the other is SELECT, then when any of them is clicked the RowCommand
event raises and
select命令可以以CommandName
多种形式提及,系统仍然会识别它。因此,例如,如果您在 中有两个Button字段GridView
,它们的第一个CommandName
是简单的select,另一个是SELECT,那么当单击它们中的任何一个时,RowCommand
事件会引发并且
if (e.CommandName == "select")
{
Label1.Text = "You clicked first button";
}
else
{
Label1.Text = "You clicked second button";
}
and the SelectedDataKey
of your GridView
will contains the data you requested. What other people offered in their answer to differ in CommandName
by setting button one to select_one
and setting button two to select_two
will help you to know if either select_one
was clicked or other, but the SelectedDataKey
of your GridView
will remain null
or DataKey
instance that doesn't contain the information you need at all.
并且SelectedDataKey
您的GridView
遗嘱包含您要求的数据。其他人在他们的回答中提供的不同之处在于CommandName
通过将按钮一设置为select_one
和将按钮二设置为select_two
将帮助您了解是否select_one
被点击或其他,但SelectedDataKey
您GridView
将保留null
或DataKey
不包含您需要的信息的实例全部。
In other words, the buttons will not select any necessary information, so it is very important to follow myanswer. It is the exact, perfect and great solution to your problem!
换句话说,按钮不会选择任何必要的信息,因此按照我的回答非常重要。它是您问题的准确、完美和出色的解决方案!