0

私は vb.net で webapp を作成しています。PostBack 後にフォーカスを別のテキスト ボックスに変更するには、UpdatePanel 内に TextBox を作成する必要があります。ViewState を使用して、読み込み時に読み込まれる数値を保存して、フォーカスがどこにあるべきかを知ることにしました (そのように機能するはずの 7 つのテキスト ボックスがあります) が、1 つだけを機能させることはできません。動作しない最小コードは次のとおりです。

     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

TextChanged イベントが実行される前に page_load メソッドが実行されるため、あなたがしていることはうまくいきません。

これを試して:

  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 によってサポートされるプロパティを Page に追加することです。

public int FocusIndex
{
    get 
    {
        object o = ViewState["foco"];
        return (o == null) ? -1 : (int) o;
    }
    set
    {
        ViewState["foco"] = value;
    }
}
于 2013-05-15T18:32:38.720 に答える
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 に答える