0

我正在使用 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
4

4 回答 4

0
 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
于 2013-05-16T16:27:50.680 回答
0

您正在做的事情不起作用,因为 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
    
于 2013-05-16T17:30:50.003 回答
0

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

    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
于 2013-05-15T18:30:45.710 回答
0

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

“不起作用”是什么意思?你预期会发生什么,实际发生了什么?

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

public int FocusIndex
{
    get 
    {
        object o = ViewState["foco"];
        return (o == null) ? -1 : (int) o;
    }
    set
    {
        ViewState["foco"] = value;
    }
}
于 2013-05-15T18:32:38.720 回答