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

发布于 2024-08-09 16:47:12 字数 1666 浏览 12 评论 0 原文

<asp:TemplateField>    
    <ItemTemplate>
        <table width="540" cellpadding="5">
        <tr>
            <td align="left" style="width:60%;">
                <img src='PurchaseHandler.ashx?ProductID=<%# Eval("ProductID")%>' 
                    alt="<%# Eval("ProductName") %>" />
            </td>
            <td align="left">
                 <h3 style="text-align:left;">
                     <asp:Label ID="nameLabel" runat="server" 
                         Text='<%# Eval("ProductName") %>'  />
                 </h3>
                 <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("Price") %>' />  
                 <br />
                 <asp:LinkButton ID="cartLink" runat="server" Text="<b>Add to Cart</b>"
                     CommandName="Add" CommandArgument='<%# Eval("ProductID") %>' />
            </td>
        </tr>
        </table>
    </ItemTemplate>
</asp:TemplateField>

我正在使用购物车业务对象,其中包含不用于在 GridView 中显示的字段。我接下来在 RowCommand 事件处理程序中尝试执行的操作是从所选行中检索其余的数据字段。这为我提供了所选行中的正确产品 ID:

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

    // Big blank!
}

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

DataRow[] rows = ds.Tables[0].Select("ProductID=" +
                     gridProducts.SelectedDataKey.Values["ProductID"].ToString());
DataRow row = rows[0];
<asp:TemplateField>    
    <ItemTemplate>
        <table width="540" cellpadding="5">
        <tr>
            <td align="left" style="width:60%;">
                <img src='PurchaseHandler.ashx?ProductID=<%# Eval("ProductID")%>' 
                    alt="<%# Eval("ProductName") %>" />
            </td>
            <td align="left">
                 <h3 style="text-align:left;">
                     <asp:Label ID="nameLabel" runat="server" 
                         Text='<%# Eval("ProductName") %>'  />
                 </h3>
                 <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("Price") %>' />  
                 <br />
                 <asp:LinkButton ID="cartLink" runat="server" Text="<b>Add to Cart</b>"
                     CommandName="Add" CommandArgument='<%# Eval("ProductID") %>' />
            </td>
        </tr>
        </table>
    </ItemTemplate>
</asp:TemplateField>

I'm using a shopping cart business object which contains fields not used for display in the GridView. What I'm attempting to do next in the RowCommand event handler is to retrieve the rest of the data fields from the selected row. This gives me the correct product ID from the selected row:

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

    // Big blank!
}

How can I grab the rest of the data fields from the selected row to populate my cart? By way of explanation, I can probably use the productID to dig into the DataSet pulled from Session state, and get the data that way. However, what I'm trying to determine is if there is a syntax similar to this that can be used in this situation?

DataRow[] rows = ds.Tables[0].Select("ProductID=" +
                     gridProducts.SelectedDataKey.Values["ProductID"].ToString());
DataRow row = rows[0];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2024-08-16 16:47:12

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

然后,您可以使用如下代码来访问您的行(代码摘自 MSDN):

  // 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];

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

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

然后您可以使用 的 FindControl() 方法GridViewRow 选择您需要的控件。

Label priceLabel = row.FindControl("priceLabel") as Label;

在 OP 发表评论后进行编辑

所以,如果我是正确的,您想访问您所在行的 DataItem 吗?

我认为这在 RowCommand 事件处理程序中是不可能的 - 我在其他论坛上做了一些挖掘,显然这个属性在 DataBind 期间不为空 - MSDN 有 说:

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

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

One way to do this would be to use the command argument to store the row index (perhaps setting it in the on row created event).

You could then use code as below to access your row (code snipped from MSDN):

  // 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];

If you need to keep your command argument as the product id then you can use the code below to access the row:

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

You can then use the FindControl() method of the GridViewRow to select the controls that you need.

Label priceLabel = row.FindControl("priceLabel") as Label;

Edit after comments from OP

So if I'm correct, you want to access the DataItem of your row?

I don't think this is possible within a RowCommand event handler - I did a little bit of digging on other forums and apparently this property is only not null during DataBind - MSDN has this to say:

The DataItem property is available only during and after the RowDataBound event of a GridView control.

Looks like your method or something similar is the only way of linking back to the original objects in the RowCommand (hopefully someone proves me wrong)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文