views:

1732

answers:

5

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

A: 

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?

Rob
It makes sense, I don't follow how I would go about doing that though?
Matt
A: 

It makes sense, I don't follow how I would go about doing that though?

Matt
A: 

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.

csgero
A: 

Thanks for all your help, this is what I have at the moment:

<asp:DetailsView OnItemCommand="DetailsView1_PageIndexChanging" ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ArtID"
    DataSourceID="AccessDataSource1" Height="50px" Width="125px">

And the VB:

    Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles DetailsView1.PageIndexChanging
    Dim AddCartID As New HttpCookie("AddCartID")
    AddCartID.Value = DetailsView1.DataKey(0)
    AddCartID.Expires = Date.Today.AddDays(1)
    Response.Cookies.Add(AddCartID)

    Response.Redirect("Purchase.aspx")
End Sub

I think I'm on the right track here this gives me the followin error though:

Compiler Error Message: BC30408: Method 'Public Sub DetailsView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.DetailsViewPageEventArgs)' does not have the same signature as delegate 'Delegate Sub DetailsViewCommandEventHandler(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)'.

Any thoughts? I've been looking up the error on Google, but I can't see any of the causes I've found on Google in mine, so I'm not sure what I'm not seeing.

Matt
A: 

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
Dillie-O