Сохраняйте фокус в текстовом поле после обратной передачи
у меня 40 TextBox с autopostback элемента управления. Каждый раз, когда срабатывает autopostback, фокус возвращается к первому текстовому полю.
Что я уже пробовал:
.ASPX <pre><tr> <td class="auto-style15">Item 1 : <asp:TextBox ID="ItemBox1" runat="server" Height="20px" Width="220px" AutoPostBack="true" TabIndex="1" MaxLength="29"></asp:TextBox><asp:Label ID="lblmessage1" runat="server" ForeColor="Red"></asp:Label></td> <td class="auto-style15">Item 2 : <asp:TextBox ID="ItemBox2" runat="server" Height="20px" Width="220px" AutoPostBack="true" TabIndex="2" MaxLength="29"></asp:TextBox><asp:Label ID="lblmessage2" runat="server" ForeColor="Red"></asp:Label></td> </tr>
.Aspx-файл.ВБ
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.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 Control = 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 System.Web.UI.WebControls.Button OrElse TypeOf c Is System.Web.UI.WebControls.ImageButton Then control = c Exit For End If Next ctl End If Return control End Function