asp.netvb.netcookiesdetailsviewbuttonfield

Using ButtonField or HyperLinkField to write a cookie in ASP.NET


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


Solution

  • 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:

    <asp:Button ID="btnAddArt" CommandName="AddCard" CommandArgument="[ArtID]" runat="server" Text="Add To Cart" />
    

    Then your code behind can function like this:

    Private Sub ProcessDetailsViewCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles DetailsView1.ItemCommand
    
    ' Using Case statement makes it easy to add more custom commands later on.
    Select Case e.CommandName
    
       Case "AddCard"
          Dim CartArtID As New HttpCookie("CartArtID")
          CartArtID.Value = Integer.Parse(e.CommandArgument.ToString)
          CartArtID.Expires = Date.Today.AddDays(0.5)
          Response.Cookies.Add(CartArtID)
          Response.Redirect("Purchase.aspx")
    
       End Select
    End Sub