在 ASP.NET 中使用 ButtonField 或 HyperLinkField 写入 cookie
我目前在 ASP.NET 中有一个 DetailsView,它根据通过 QueryString 传递的 ID 从数据库获取数据。 我现在一直在尝试做的是在用户单击 ButtonField 或 HyperLinkField 时创建的新 cookie 中使用相同的 ID。
我在 .aspx 中的内容是这样的:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ArtID"
DataSourceID="AccessDataSource1" Height="50px" Width="125px">
<Fields>
<asp:ImageField DataAlternateTextField="Title" DataImageUrlField="FileLocation">
</asp:ImageField>
<asp:BoundField DataField="ArtID" HeaderText="ArtID" InsertVisible="False" ReadOnly="True"
SortExpression="ArtID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="ArtDate" HeaderText="ArtDate" SortExpression="ArtDate" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="FileLocation" HeaderText="FileLocation" SortExpression="FileLocation" />
<asp:BoundField DataField="Medium" HeaderText="Medium" SortExpression="Medium" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="PageViews" HeaderText="PageViews" SortExpression="PageViews" />
<asp:HyperLinkField DataNavigateUrlFields="ArtID" DataNavigateUrlFormatString="Purchase.aspx?ArtID={0}"
NavigateUrl="Purchase.aspx" Text="Add To Cart" />
<asp:ButtonField ButtonType="Button" DataTextField="ArtID" Text="Add to Cart" CommandName="btnAddToCart_Click" />
</Fields>
</asp:DetailsView>
当使用调节器 asp.net 按钮时,例如:
<asp:Button ID="btnAddArt" runat="server" Text="Add To Cart" />
我在 VB 中会有类似的内容:
Protected Sub btnAddArt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddArt.Click
Dim CartArtID As New HttpCookie("CartArtID")
CartArtID.Value = ArtID.DataField
CartArtID.Expires = Date.Today.AddDays(0.5)
Response.Cookies.Add(CartArtID)
Response.Redirect("Purchase.aspx")
End Sub
但是,我不知道如何将其应用到 ButtonField 中,因为ButtonField 不允许我给它一个 ID。
我需要添加到 cookie 的 ID 是第一个 BoundField 中的 ArtID。
非常感谢任何有关我如何执行此操作的想法/建议!
或者,如果我可以使用 HyperLinkField 或常规按钮来完成此操作,那也同样好,但我在使用常规按钮访问 DetailsView 中的 ID 时遇到了麻烦。
谢谢
I currently have a DetailsView in ASP.NET that gets data from the database based on an ID passed through a QueryString. What I've been trying to do now is to then use that same ID in a new cookie that is created when a user clicks either a ButtonField or a HyperLinkField.
What I have in the .aspx is this:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ArtID"
DataSourceID="AccessDataSource1" Height="50px" Width="125px">
<Fields>
<asp:ImageField DataAlternateTextField="Title" DataImageUrlField="FileLocation">
</asp:ImageField>
<asp:BoundField DataField="ArtID" HeaderText="ArtID" InsertVisible="False" ReadOnly="True"
SortExpression="ArtID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="ArtDate" HeaderText="ArtDate" SortExpression="ArtDate" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="FileLocation" HeaderText="FileLocation" SortExpression="FileLocation" />
<asp:BoundField DataField="Medium" HeaderText="Medium" SortExpression="Medium" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="PageViews" HeaderText="PageViews" SortExpression="PageViews" />
<asp:HyperLinkField DataNavigateUrlFields="ArtID" DataNavigateUrlFormatString="Purchase.aspx?ArtID={0}"
NavigateUrl="Purchase.aspx" Text="Add To Cart" />
<asp:ButtonField ButtonType="Button" DataTextField="ArtID" Text="Add to Cart" CommandName="btnAddToCart_Click" />
</Fields>
</asp:DetailsView>
When using a reguler asp.net button such as:
<asp:Button ID="btnAddArt" runat="server" Text="Add To Cart" />
I would have something like this in the VB:
Protected Sub btnAddArt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddArt.Click
Dim CartArtID As New HttpCookie("CartArtID")
CartArtID.Value = ArtID.DataField
CartArtID.Expires = Date.Today.AddDays(0.5)
Response.Cookies.Add(CartArtID)
Response.Redirect("Purchase.aspx")
End Sub
However, I can't figure out how I go about applying this to the ButtonField instead since the ButtonField does not allow me to give it an ID.
The ID that I need to add to the cookie is the ArtID in the first BoundField.
Any idea's/advice on how I would go about doing this are greatly appreciated!
Alternatively, if I could do it with the HyperLinkField or with the regular button, that would be just as good, but I'm having trouble using a regular button to access the ID within the DetailsView.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Button 类的 CommandName 和 CommandArgument 参数。 指定 CommandName 将公开 ItemCommand 事件。 从那里您可以检查 CommandName,轻松获取 CommandArgument(行或项目的 ID),然后以这种方式将您需要的任何数据推送到 cookie 中。
更正式地说,您希望有这样的按钮:
然后您的代码可以像这样运行:
Use the CommandName and the CommandArgument parameters of the Button class. Specifying a CommandName will expose the ItemCommand event. From there you can check for the CommandName, easily grab the CommandArgument (the ID of your row or item) then push whatever data your need into your cookie that way.
More formally, you're looking to have your button like this:
Then your code behind can function like this:
我注意到您将密钥放在网格本身中(DataKeyNames =“ArtID”)。 您可以在事件处理程序中访问它——事件参数将为您提供当前索引,用于索引网格上的数据键。
合理?
I noticed you're putting the key in the grid itself (DataKeyNames="ArtID"). You have access to that in your event handler -- the event args will get you the current index for indexing into the datakeys on the grid.
Make sense?
由于您设置了DetailsView 控件的DataKeyNames 属性,因此您可以使用DetailsView1.DataKey(0) 访问所显示项目的ArtID。 或者,您可以使用DetailsView1.SelectedValue 来获得相同的结果。
至于处理单击事件,您必须将 ItemCommand 事件处理程序添加到 DetailsView。
Since you set the DataKeyNames property of the DetailsView control, you can access the ArtID of the displayed item using the DetailsView1.DataKey(0). Alternatively you can use DetailsView1.SelectedValue to get the same.
As for handling the click event, you'll have to add an ItemCommand event handler to the DetailsView.