使用文本更改在回发后维护 Tab 索引



我有 3 个文本框具有 AutoPostBack = true。在回发时,它失去了焦点。我一直在寻找并尝试了很多东西,但没有任何效果。

下面的代码是我正在尝试执行的操作的片段。

基本上,第一个片段没有抓住任何东西。有谁知道我在这里做错了什么

**旁注我没有使用更新面板,我使用的是布局项嵌套控件容器

Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control



Protected Sub txtDEPTH_TextChanged(sender As Object, e As EventArgs) Handles txtDEPTH.TextChanged
UpdateProductTemp()
txtDEPTH.Focus()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim wcICausedPostBack As WebControl = CType(GetControlThatCausedPostBack(TryCast(sender, Page)), WebControl)
Dim indx As Integer = wcICausedPostBack.TabIndex
Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus()
End If

End Sub
Protected Function GetControlThatCausedPostBack(ByVal page As Page) As Control
Dim control As WebControl = Nothing
Dim ctrlname As String = page.Request.Params.[Get]("__EVENTTARGET")
If ctrlname IsNot Nothing AndAlso ctrlname <> String.Empty Then
control = page.FindControl(ctrlname)
Else
For Each ctl As String In page.Request.Form
Dim c As Control = page.FindControl(ctl)
If TypeOf c Is TextBox OrElse TypeOf c Is DropDownList Then
control = c
Exit For
End If
Next
End If
Return control
End Function

您使用的是哪个版本?因为,当我尝试设置 Textbox 的 focus(( 时,编译器给了我一个错误,因为没有这样的方法。但是,HTML 提供了一个属性,该属性会自动聚焦可聚焦的元素。

溶液:

TextBoxToSetFocus.Attributes.Add("autofocus", "")

问候

马赫什瓦拉

最新更新