【问题标题】:ViewState not saving values during PostBackViewState 在 PostBack 期间不保存值
【发布时间】:2013-05-15 17:58:56
【问题描述】:

我正在使用 vb.net 制作一个 webapp,我需要在 UpdatePanel 中制作一个 TextBox,以便在 PostBack 之后将焦点更改为另一个文本框。我决定使用 ViewState 来保存一个数字,该数字将在加载时读取以了解焦点应该在哪里(应该有七个文本框应该像这样工作),但我不能只做一个工作。 这是不起作用的最小代码。

     Dim g As Integer
    g = 1
    ViewState.Add("foco", g)

这里是 Page_Load。

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    If Page.IsPostBack Then
        If ViewState("foco") = 1 Then
            TextBox1.Focus()
        End If
    End If

End Sub

【问题讨论】:

    标签: vb.net viewstate


    【解决方案1】:

    看起来您没有在回发之间增加计数器。

        If Page.IsPostBack Then
            If ViewState("foco") = 1 Then
                TextBox1.Focus()
            ElseIf ViewState("foco") = 2 Then
                TextBox2.Focus()
            ElseIf ViewState("foco") = 3 Then
                TextBox3.Focus()
            End If
            ViewState("foco") = ViewState("foco") + 1
        Else
            ViewState.Add("foco", 1)
        End If
    

    【讨论】:

      【解决方案2】:

      向 ViewState 添加值的代码何时执行?

      “不工作”是什么意思?您预期会发生什么,实际发生了什么?

      无论如何,最简单的方法可能是向您的页面添加一个由 ViewState 支持的属性,例如:

      public int FocusIndex
      {
          get 
          {
              object o = ViewState["foco"];
              return (o == null) ? -1 : (int) o;
          }
          set
          {
              ViewState["foco"] = value;
          }
      }
      

      【讨论】:

      • 它是 UpdatePanel 上的一个 TextBox,当按下回车键时,它应该用数据库中的结果填充一个 List,然后在回发返回时保存要关注的文本框的数量。我在下面发布了代码。
      【解决方案3】:
       Protected Sub TextBox7_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
          If TextBox7.Text = "" Then Exit Sub
      
      
          '  ListBox1.Visible = True
      
      
          ListBox1.Items.Clear()
      
      
      
          Dim con As New Data.OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=TEST08\AXSQLEXPRESS;Password=Axoft1988;User ID=sa;Initial Catalog=club_independiente")
          con.Open()
      
          'Dim com As New Data.OleDb.OleDbCommand("select * from emCaja where cod_client = '" & TextBox1.Text & "'", con)
          ' If "" & com.ExecuteScalar() = "" Then
          Dim com As New Data.OleDb.OleDbCommand
      
          com = New Data.OleDb.OleDbCommand("select * from emConceptos where codigo = " & TextBox7.Text, con)
          com.ExecuteNonQuery()
      
          Dim dr As Data.OleDb.OleDbDataReader
          dr = com.ExecuteReader
      
          While dr.Read
              ListBox1.Items.Add(dr("descripcion"))
              ListBox1.Items(ListBox1.Items.Count - 1).Value = dr("codigo")
          End While
          dr.Close()
      
          ' ListBox1.Focus()
      
          If ListBox1.Items.Count > 0 Then
              ListBox1.SelectedIndex = 0
          End If
          Dim g As Integer
          g = 1
          Session("foco") = g
      
      End Sub
      

      【讨论】:

        【解决方案4】:

        你正在做的事情不起作用,因为 page_load 方法在 TextChanged 事件有机会执行之前运行。

        试试这个:

        1. 在您的页面中添加脚本管理器;
        2. 将你的 page_load 逻辑放在 page_preRender 事件中,保证在 textchanged 事件之后触发;

          Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
          
              If Page.IsPostBack Then
                  If ViewState("foco") = 1 Then
                      ScriptManager1.SetFocus(TextBox1)
                  End If
              End If
          
          End Sub
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-15
          • 2012-08-13
          • 2011-10-30
          • 2015-01-11
          • 1970-01-01
          • 2011-03-17
          相关资源
          最近更新 更多