Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView控件,具有两个asp:Command列,使用相同的命令_C#_Asp.net_Gridview - Fatal编程技术网

C# GridView控件,具有两个asp:Command列,使用相同的命令

C# GridView控件,具有两个asp:Command列,使用相同的命令,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个ASP.NETGridView控件,其中有两个ASP:CommandField列,它们都使用Select命令执行不同的任务。当我检查GridViewCommandEventArgs对象的CommandName属性时,如果两者都返回“Select”,如何区分在OnRowCommand事件中选择的列 以下是我的源代码: ASPX页面: <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnR

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

以下是我的源代码:

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>

使用.CommandArgument属性

使用按钮列,这样您就可以指定特定的命令名并从此处使用它

<asp:ButtonField ButtonType="Link" Text="Click Me" CommandName="MyCommand1" />
<asp:ButtonField ButtonType="Link" Text="No Click Me" CommandName="MyCommand2" />


然后,您可以根据e.CommandName采取操作。首先,您必须使用SELECt作为命令吗?你可以把它设置成其他更有意义的东西


其次,您可以将CommandArgument属性设置为不同的值,以便知道单击的是哪个值。

使用command argument属性

e.commandargument
或者,您可以在命令字段中动态创建按钮,并将commandName设置为您想要的任何内容

在gridview中加载

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

for(int i=0;i你寻求的答案既简单又棘手。我在我的网站项目中也遇到了这个问题,所以我上网冲浪了好几天,却找不到我和你需要的东西

有一天,我独自思考了这个问题,做了几个小时的实验,终于意识到知道按钮被点击的列的唯一简单方法是在
GridView的
RowCommand事件中
,在
GridView commandeventargs的
CommandName属性中通过设计模式编辑
GridView
的列,并将命令字段替换为按钮字段,这是一种简单而舒适的方法

您可以在其
CommandName
中为每个按钮字段提供任何
string/text
,因此在
RowCommand
事件中,您可以知道单击了哪个按钮。
Button1
Button2
,但您不想提供这些字符串,因为您请求了按钮应该选择并提供给您的内容,所以
CommandName
应该是单词select,但它也可以是selectselect

选择命令可以在
CommandName
中以多种形式出现,但系统仍会识别它。例如,如果在
GridView
中有两个按钮字段,其第一个
CommandName
只是选择,另一个是选择,则当单击m,则
行命令
事件将引发

if (e.CommandName == "select")
  { 
   Label1.Text = "You clicked first button";
  }
  else 
  {
   Label1.Text = "You clicked second button";
  }
您的
GridView
SelectedDataKey
将包含您请求的数据。其他人在回答中提供了哪些不同的
CommandName
,方法是将按钮一设置为
SelectOne
,将按钮二设置为
SelectOne
将帮助您知道是否有任何一个
SelectOne
icked或其他,但是
GridView
SelectedDataKey
将保持
null
DataKey
实例,该实例根本不包含您需要的信息


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

我尝试了CommandName属性,但出现以下错误:键入“System.Web.UI.WebControls.CommandField”没有名为“Co”的公共属性mmandName‘要做到这一点,您必须动态创建它。请看我上面给出的答案。@Michael-确保您使用的是我更新的示例,它是一个ButtonColumn@Eric使用它不需要动态创建任何内容,只需使用ButtonColumn即可。使用Select命令是有意义的,因为我不执行任何操作对记录执行CRUD操作。这两种情况都只需要一个信息查询。但是命令可以是任何东西。例如,它可以是select_one或select_two…我的问题的全部目的是试图确定是否有方法表明已单击select_one。是的,您可以将命令名设置为select_one。或者您可以将CommandArgument属性设置为1。无论哪种方式,您都可以检查单击了哪个命令…CommandArgument属性返回所选行的索引。我需要知道行中选择了哪个asp:commandfield。否,CommandArgument返回您指定的任何参数。因此,您可以使用相同逗号的按钮ndName,但参数不同。这有助于确定是哪个按钮引发了该命令。在这种用户情况下,手动添加链接是多余的,需要对不需要的网格项进行二次遍历。
if (e.CommandName == "select")
  { 
   Label1.Text = "You clicked first button";
  }
  else 
  {
   Label1.Text = "You clicked second button";
  }