我有一个使用UpdatePanel
. 控件本质上是一个带有Button
. 当用户单击 时Button
,会打开一个模式弹出窗口,允许他们选择一些值。表的数据(使用 aRepeater
作为其DataSource
)作为对象列表存储在部分回发(UpdatePanel
触发时)之间的会话变量中。如果我只有一个控件,则一切正常,但如果我在同一页面中多次使用此控件,则会话变量中的对象列表将组合在一起,并且不会为每个控件分开。我认为这可能是因为会话变量名称不是唯一的,所以在我调用或使用变量的任何地方,我都会这样做:
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 事件)中删除某些内容时,该对象将从列表中删除,并重新添加到会话中。如果有任何原因导致通知程序 2 的更新面板触发,它将显示与通知程序 1 完全相同的数据