简体   繁体   中英

ASP.NET: Storing Repeater Value in Session

I want to store the value of a databound repeat to a session state.

This is my code so far:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1 id="price" class="QPrice">$<%# Eval("Price")%></h1>
</ItemTemplate>
</asp:Repeater>

Code Behind"

Session("Qprice") = ?

How do I retrive the value of the h1 element or retrive a specific value of SqlDatasource1, from the codebehind?

Session("Qprice"+ID) = (decimal)drv.Row["Price"]; In the end you will have all prices in session, and you can access by the id's of the data that has bound to the list. But you are storing information that you have access.

In the repeater1 define the object DatakeyNames="Price" and specify the Item Data Bound event then in code behind

protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item 
                          || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
        Button b = e.Item.FindControl("myButton") as Button;  
        DataRowView drv = e.Item.DataItem as DataRowView; 
        Session("Qprice") = (decimal)drv.Row["Price"];
    } 
} 

Here you can save the value or do whatever you want with price.

I would store it to strongly type List first and store it in Session

Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    Dim repeaterItems As New List(Of Decimal)()

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim b As Button = TryCast(e.Item.FindControl("myButton"), Button)
        Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)


        repeaterItems.Add(CDec(drv.Row("Price")))
    End If


    Session("Qprice") = repeaterItems
End Sub

So I can access it again later like

Dim repeaterItemsFromSession As List(Of Decimal) = DirectCast(Session("Qprice"), List(Of Decimal))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM