简体   繁体   English

在C#中,如何基于在gridview行中单击的按钮引用特定的产品记录

[英]In C#, how can I reference a specific product record based on a button that's clicked in a gridview row

I have a page that displays a gridview of products. 我有一个页面,显示产品的gridview。 Inside this table is a column with a hyperlink called "Details." 该表内部是一列,其中包含一个称为“详细信息”的超链接。 I want to make it so that if the user clicks the details cell for that specific product, a new page will open that gives more information on that product. 我要这样做,以便如果用户单击该特定产品的详细信息单元,将打开一个新页面,其中提供了有关该产品的更多信息。 I'm not sure how I would determine which Product record the details link is in and how I would carry that over to the next page. 我不确定如何确定详细信息链接位于哪个Product记录中,以及如何将其带到下一页。

You use a HyperlinkField which uses the ID of the product in the querystring. 您使用HyperlinkField,它在查询字符串中使用产品的ID。

Here's a sample: 这是一个示例:

<asp:GridView runat="server" ID="gridViewProducts">
  <Columns>
     <asp:HyperLinkField HeaderText="ProductId" DataNavigateUrlFormatString="ProductDetail.aspx?action=edit&id={0}" DataNavigateUrlFields="ProductId" DataTextField="ProductId"  />
     <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
  </Columns>
</asp:GridView>

When you bind, the productID automatically goes into the querystring 绑定时,productID自动进入查询字符串

As far as "linking" your product records, programmers do this using unique identifiers. 至于“链接”您的产品记录,程序员使用唯一的标识符来完成。 In most circumstances, you'll use an integer. 在大多数情况下,您将使用整数。

Historically, people have used the query string to pass database IDs from one page to another. 历史上,人们使用查询字符串将数据库ID从一页传递到另一页。 In ASP.NET, you can do this as well. 在ASP.NET中,您也可以这样做。 Alternatively, you could store the product ID in the Session and then load that ID from the Session in your new page. 或者,您可以在Session存储产品ID,然后在新页面中从Session中加载该ID。

You can take the productId by index property of the gridview. 您可以通过gridview的index属性获取productId。 Using that productId, you can navigate to the details of a particular product. 使用该productId,您可以导航到特定产品的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按钮单击行时,在C#Gridview中获取单元格值 - Get Cell Value in C# Gridview when Button clicked on row 如何根据Gridview Row单击动态更改Button的文本? - How can I change my Button's text dynamically based on my Gridview Row click? 在C#中单击GridView行后如何填充文本框 - How to Populate Textbox after GridView row clicked in C# 如何使用C#确定在GridView中单击的行 - How to determine which row is clicked in GridView using c# c# asp.net 当我单击 gridview 中的按钮时,如何更改按钮的图像 url(其类型为图像) - c# asp.net how can i change the image url of button (it's type is image ) when i click the button in gridview 在gridview中单击“编辑”按钮时,如何调用“删除行”事件? asp.net C# - How can I call “delete row ” event when i click on “edit” button in gridview? asp.net C# C# WinUI3 桌面:如何引用或控制 ListView 的所选项目 DataTemplate 中的按钮? - C# WinUI3 Desktop: How can I reference or control a button in a ListView's selected item DataTemplate? 如何在C#中更新SQLite记录的属性? - How can I update a SQLite record's attributes in C#? 如何基于行数据在ac#.NET gridview中设置分页? - How do I set up paging in a c# .NET gridview based on row data? 删除GridView中1行C#的选择按钮 - Remove Select Button in GridView for 1 row C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM