回发后保持文本框中的焦点 [英] Maintain focus in textbox after postback

查看:163
本文介绍了回发后保持文本框中的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有40个自动回复的文本框。每当autopostback触发时,焦点将返回到第一个文本框。



我尝试过:



i have 40 textbox with autopostback. Everytime autopostback is trigger the focus will go back to the first textbox.

What I have tried:

.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.VB



.ASPX.VB

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

推荐答案

试试这个

i用c#



try this
i have written in c#

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) {
                string targetId = Page.Request.Params.Get("__EVENTTARGET");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "focusthis", "document.getElementById('" + targetId + "').focus()", true);

            }
        }


这篇关于回发后保持文本框中的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆