我有一个使用UpdatePanel
的用户控件。这些控件基本上是一张Button
的表格。当用户点击Button
时,会打开一个模式弹出窗口,允许他们选择某个值。表(使用Repeater
作为其DataSource
)的数据作为对象列表存储在部分回发(发生时为UpdatePanel
)之间的会话变量中。一切工作正常,如果我只有一个控件,但如果我多次在同一页面中使用此控件,会话变量中的对象列表会合并在一起,并且不会为每个控件分开。我想这可能是因为会话变量名称不是唯一的,所以无论我打电话或使用变量,我不喜欢这样写道:Asp.net会话变量混合起来
Dim sessionName as string = Me.UniqueID & "_" & "userNotificationDS"
Session(sessionName) = myListOfObjects
但是,这并没有改变结果。任何人都知道我可能在这里做错了什么?如果您认为完整的代码会有所帮助,请告诉我。
控制服务器代码:
Protected Sub delete_click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As LinkButton = CType(sender, LinkButton)
Dim ds As New List(Of myObject)
sessionName = Me.UniqueID & "_" & "myDataSet"
ds = Session(sessionName.ToString)
Dim id As String = btn.CommandArgument
ds.RemoveAll(Function(userNotification) userNotification.User.NetworkID.Equals(id))
Session(sessionName.ToString) = ds
bindData(ds)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
sessionName = Me.UniqueID & "_" & "myDataSet"
If (Session(sessionName.ToString) IsNot Nothing) Then
bindData(Session(sessionName.ToString))
End If
End Sub
Private Function buildPagedSet(ByVal userNotification As List(Of myObject)) As PagedDataSource
Dim ps As PagedDataSource = New PagedDataSource()
ps.DataSource = userNotification
ps.AllowPaging = True
ps.PageSize = numRows
Return ps
End Function
Public Sub bindData(ByVal commentList As List(Of myObject))
sessionName = Me.UniqueID & "_" & "myDataSet"
Dim currentPage As Integer = 0
Dim ps As PagedDataSource
Dim numLable As Label
Dim denomLable As Label
Dim curPage As Integer = 1
Dim totalPage As Integer = 0
If (Not myObject Is Nothing) Then
Try
ps = buildPagedSet(commentList)
totalPage = ps.PageCount
Session(sessionName.ToString) = commentList
rowTotal = ps.Count
'for paging
If Not (ViewState(Me.UniqueID & "_Page") Is Nothing) Then
currentPage = Convert.ToInt32(ViewState(Me.UniqueID & "_Page"))
Else
ViewState(Me.UniqueID & "_Page") = 1
currentPage = 1
End If
If (currentPage > 0 And currentPage <= ps.PageCount) Then
ps.CurrentPageIndex = currentPage - 1
Me.dataRepeateUsers.DataSource = ps
Me.dataRepeateUsers.DataBind()
ElseIf (currentPage >= ps.PageCount) Then
ViewState(Me.UniqueID & "_Page") = Convert.ToInt32(ViewState(Me.UniqueID & "_Page")) - 1
ElseIf (currentPage <= 0) Then
ViewState(Me.UniqueID & "_Page") = Convert.ToInt32(ViewState(Me.UniqueID & "_Page")) + 1
Else
End If
Catch ex As Exception
Throw
End Try
Else
Dim emptySet As New List(Of myObject)
Me.dataRepeateUsers.DataSource = emptySet
Me.dataRepeateUsers.DataBind()
End If
End Sub
控制被实例化这样的:
Me.notifier1.bindData(notificationList)
在这个例子中,当用户删除从notifier1东西(在delete_click事件)对象从除去该列表并将其添加回会话。如果有什么原因notifier2的更新面板着火,它会显示完全相同的数据notifier1
我也为所有这些事件添加了断点。代码运行时(即用户删除一条记录)没问题。我可以看到会话密钥和数据集以及它们的匹配。这里没有混淆。只有当其他控件更新面板触发时。然后,在OnLoad事件中,我可以看到两个会话密钥都获得相同的数据集 – jason 2013-02-14 12:21:05