使用相同命令的命令列和两个asp:命令列的GridView控件

时间:2022-05-10 14:49:20

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?

我有一个ASP。使用两个asp:CommandField列的NET GridView控件,它们都使用Select命令来执行不同的任务。当我检查GridViewCommandEventArgs对象的CommandName属性时,当两者都返回“Select”时,如何区分OnRowCommand事件中选择的列?

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
}   

5 个解决方案

#1


4  

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

然后您可以基于. commandname操作。

#2


0  

Use the GridViewCommandEventArgs.CommandArgument property !

使用GridViewCommandEventArgs。CommandArgument财产!

#3


0  

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属性设置为不同的值,从而知道要单击哪个值。

#4


0  

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

#5


0  

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.

有一天,我只考虑了这个问题,并做了几个小时的实验,终于意识到,知道按钮被单击的那一列的唯一简单方法是在GridViewCommandEventArgs的CommandName属性的GridView的RowCommand事件中。更正确、更舒适的方法是通过设计模式编辑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 SELECT too and Select and selecT and etc.

您可以在命令名中为每个按钮字段提供您想要的任何字符串/文本,因此在RowCommand事件中您可以知道被单击的是哪个。Button1或Button2,但你不想给出这些字符串,因为你需要一些按钮应该选择并给你的东西,所以CommandName应该是单词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 Button fields in the GridView, whose first CommandName is simply select and the other is SELECT, then when any of them is clicked the RowCommand event raises and

select命令可以在许多形式的CommandName中提到,但是系统仍然会识别它。例如,如果GridView中有两个按钮字段,它们的第一个命令名是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.

GridView的SelectedDataKey将包含您所请求的数据。别人提供的答案不同CommandName通过设置按钮一个select_one和设置按钮两个select_two将帮助你知道select_one是点击或其他,但显示数据表格的SelectedDataKey仍将是null或DataKey实例不包含你需要的信息。

In other words, the buttons will not select any necessary information, so it is very important to follow my answer. It is the exact, perfect and great solution to your problem!

换句话说,按钮不会选择任何必要的信息,所以遵循我的答案是非常重要的。它是你问题的准确、完美和伟大的解决方案!

#1


4  

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

然后您可以基于. commandname操作。

#2


0  

Use the GridViewCommandEventArgs.CommandArgument property !

使用GridViewCommandEventArgs。CommandArgument财产!

#3


0  

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属性设置为不同的值,从而知道要单击哪个值。

#4


0  

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

#5


0  

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.

有一天,我只考虑了这个问题,并做了几个小时的实验,终于意识到,知道按钮被单击的那一列的唯一简单方法是在GridViewCommandEventArgs的CommandName属性的GridView的RowCommand事件中。更正确、更舒适的方法是通过设计模式编辑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 SELECT too and Select and selecT and etc.

您可以在命令名中为每个按钮字段提供您想要的任何字符串/文本,因此在RowCommand事件中您可以知道被单击的是哪个。Button1或Button2,但你不想给出这些字符串,因为你需要一些按钮应该选择并给你的东西,所以CommandName应该是单词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 Button fields in the GridView, whose first CommandName is simply select and the other is SELECT, then when any of them is clicked the RowCommand event raises and

select命令可以在许多形式的CommandName中提到,但是系统仍然会识别它。例如,如果GridView中有两个按钮字段,它们的第一个命令名是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.

GridView的SelectedDataKey将包含您所请求的数据。别人提供的答案不同CommandName通过设置按钮一个select_one和设置按钮两个select_two将帮助你知道select_one是点击或其他,但显示数据表格的SelectedDataKey仍将是null或DataKey实例不包含你需要的信息。

In other words, the buttons will not select any necessary information, so it is very important to follow my answer. It is the exact, perfect and great solution to your problem!

换句话说,按钮不会选择任何必要的信息,所以遵循我的答案是非常重要的。它是你问题的准确、完美和伟大的解决方案!