如何清除会话(“数据表")值 [英] How to clear Session("Datatable") values

查看:110
本文介绍了如何清除会话(“数据表")值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从gridview中清除会话值.

我在会话thorugh数据表中遇到网格值问题.

每当我过滤时,我都会得到旧的价值.

因为这些值在会话中保持不变.

我希望每当单击searchig时,都应获取新鲜的会话网格值.

怎么做...

看到我的代码
------------

How to clear session values from gridview.

I am getting problem in grid values in session thorugh datatable.

whenever i am filtering i am getting the old valus.

because, the values are maintaing in the session.

I want whenever click the searchig, it should fetch the the fresh session grid values.

How to do this...

see my code
------------

Private Property DataTable() As DataTable
      Get

          Return DirectCast(Session("DataTable"), DataTable)
      End Get
      Set(ByVal value As DataTable)

          Session("DataTable") = value
      End Set
  End Property
  Protected Overrides Sub OnLoad(ByVal e As EventArgs)
      MyBase.OnLoad(e)


      If Not Me.IsPostBack Then
          Dim dt As DataTable
          If Me.DataTable Is Nothing Then
              dt = InlineAssignHelper(Me.DataTable, Me.LoadData())
          Else
              dt = Me.DataTable
          End If
          Me.GridView3.DataSource = dt
          Me.GridView3.DataBind()
          Me.txtSearch.Attributes.Add("onkeyup", String.Format("javascript:__doPostBack('{0}','')", Me.upnlGridView.ClientID))
      Else

          Dim target As String = Me.Request.Form("__EVENTTARGET")
          If Not String.IsNullOrEmpty(target) AndAlso target.Equals(Me.upnlGridView.ClientID) Then
              If Not String.IsNullOrEmpty(Me.txtSearch.Text) Then
                  Dim dt As DataTable = Me.GetTable()
                  For i As Integer = 0 To dt.Rows.Count - 1
                      dt = DirectCast(Session("DataTable"), DataTable)
                      Dim dr As DataRow = dt.Rows(i)
                      dt.Rows.Remove(dr)
                  Next
                  GridView3.DataBind()

                  Dim rows As DataRow() = Me.DataTable.[Select](String.Format("Emp_Number LIKE '%{0}%'", Me.txtSearch.Text))
                  Me.GridView3.DataSource = Me.LoadData(rows)
                  Me.GridView3.DataBind()

              Else
                  Me.GridView3.DataSource = Me.DataTable
                  Me.GridView3.DataBind()
              End If
          End If
      End If
  End Sub
  Public Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
      target = value
      Return value
  End Function
  Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
  End Sub
  Protected Sub Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
  End Sub
  Private Function LoadData() As DataTable
      Return Me.LoadData(Nothing)
  End Function

推荐答案

如果您检查代码,则会看到您仅在if语句的!IsPostback部分中设置了DataTable属性(用于设置会话值).如果需要新数据,则在处理完其他数据后,还需要用Else块中的新值更新DataTable.
If you check your code, you''ll see that you only set the DataTable property(which sets the session value) in the !IsPostback portion of the if statement. If you want new data, then you need to update DataTable with the new value in the Else block as well once you are done working with it.


使用
Session.Clear()


清除会话中存储的所有值.

但是如果您想清除会话中的某些特定密钥,请覆盖该密钥


to clear all the values stored in session.

but if you want to clear some specific key in session, then over-write the key

Session.Add("Mykeyname","")


这篇关于如何清除会话(“数据表")值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆