Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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/7/css/41.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
Asp.net GridView模板-如何从选定行获取数据_Asp.net_Gridview_Templatefield_Rowcommand - Fatal编程技术网

Asp.net GridView模板-如何从选定行获取数据

Asp.net GridView模板-如何从选定行获取数据,asp.net,gridview,templatefield,rowcommand,Asp.net,Gridview,Templatefield,Rowcommand,如何从所选行获取其余数据字段以填充购物车?作为解释,我可能可以使用productID来挖掘从会话状态提取的数据集,并以这种方式获取数据。但是,我想确定的是,在这种情况下是否有类似的语法可以使用 if (e.CommandName == "Add") { int productID = 0; int.TryParse(e.CommandArgument as string, out productID); // Big blank! } 一种方法是使用命令参数存储行索引

如何从所选行获取其余数据字段以填充购物车?作为解释,我可能可以使用productID来挖掘从会话状态提取的数据集,并以这种方式获取数据。但是,我想确定的是,在这种情况下是否有类似的语法可以使用

if (e.CommandName == "Add")
{
    int productID = 0;
    int.TryParse(e.CommandArgument as string, out productID);

    // Big blank!
}

一种方法是使用命令参数存储行索引(可能在on row created事件中设置它)

然后,您可以使用下面的代码访问您的行(代码从中截取):

如果需要将命令参数保留为产品id,则可以使用以下代码访问该行:

  // Convert the row index stored in the CommandArgument
  // property to an Integer.
  int index = Convert.ToInt32(e.CommandArgument);

  // Retrieve the row that contains the button clicked
  // by the user from the Rows collection. Use the
  // CommandSource property to access the GridView control.
  GridView customersGridView = (GridView)e.CommandSource;
  GridViewRow row = customersGridView.Rows[index];
然后可以使用
GridViewRow
的FindControl()方法来选择所需的控件

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
在OP评论后编辑

如果我是对的,您想访问此行的数据项吗

我认为这在RowCommand事件处理程序中是不可能的-我在其他论坛上做了一些挖掘,显然这个属性仅在
数据绑定期间不为null
-MSDN必须说明:

DataItem属性仅在GridView控件的RowDataBound事件期间和之后可用


看起来您的方法或类似方法是在RowCommand中链接回原始对象的唯一方法(希望有人证明我错了)

你好,David,谢谢您的输入。我编辑了这个问题,进一步解释了我要查找的是数据集中的其他非显示字段。我可以通过使用我已经获得的productID来访问它们。然而,有些事情告诉我,有一个更优雅的方法来解决这个问题。。。在这种情况下,TemplateFields总是让我感到困惑。安东尼:-)谢谢大卫,这就是答案!我能够直接从数据集获取数据。Anthony:-)为了其他人的利益,我通过循环数据集的表行来获得所选行的索引,以获得先前获得的productID的匹配。然后我可以访问其余的数据。
  // Convert the row index stored in the CommandArgument
  // property to an Integer.
  int index = Convert.ToInt32(e.CommandArgument);

  // Retrieve the row that contains the button clicked
  // by the user from the Rows collection. Use the
  // CommandSource property to access the GridView control.
  GridView customersGridView = (GridView)e.CommandSource;
  GridViewRow row = customersGridView.Rows[index];
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Label priceLabel = row.FindControl("priceLabel") as Label;