ASP.NET:在会话中存储转发器值

时间:2012-04-25 00:17:51

标签: asp.net session repeater

我想将数据绑定重复的值存储到会话状态。

到目前为止,这是我的代码:

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

背后的代码“

Session("Qprice") = ?

如何从代码隐藏中检索h1元素的值或者检索SqlDatasource1的特定值?

3 个答案:

答案 0 :(得分:1)

会话(“Qprice”+ ID)=(十进制)drv.Row [“Price”]; 最后,您将获得会话中的所有价格,并且您可以通过绑定到列表的数据的ID来访问。但是您正在存储您有权访问的信息。

答案 1 :(得分:0)

在repeater1中定义对象DatakeyNames =“Price”并指定Item Data Bound事件 然后在代码背后

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"];
    } 
} 

在这里,您可以保存价值或以价格做任何您想做的事。

答案 2 :(得分:0)

我会先将它存储到强类型List并将其存储在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

所以我可以稍后再次访问它,如

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