用户提交表单后的焦点文本框

Focus Textbox After User Submits Form

根据我的编码,我想关注标记消息框的文本框。

如果我在 Response.Write 之后执行 FocusText = Password2Response.Write 将不起作用。

因此我尝试为 FocusText 创建一个全局变量(并删除 Response.Write 之后的行),但是 Password1 的 "Page load" 声明似乎无论如何都会覆盖它。我该如何纠正?

我想要的示例:

-如果用户不填写 Company Namefederal tax 等,情况相同....

Public Class Supplier
Inherits System.Web.UI.Page
Public Shared FocusText As TextBox

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'LocalUnionText.Text.Visible = False
    'Dim focusP As TextBox = Password2
    'focusP.Focus()
    FocusText = Password1

    FocusText.Focus()
End Sub

Protected Sub Register_Click(sender As Object, e As EventArgs) Handles Register.Click
    Dim tax_id_text As String
    tax_id_text = FED_TAX_ID.Text

    Dim FocusText As TextBox
    Dim FocusDrop As DropDownList

    If Password1.Text.Length <> 0 Then
        'Password2
        If Password2.Text.Length <> 0 Then
            'FedTax
            If FED_TAX_ID.Text.Length <> 0 Then
                If COMPANY_NAME.Text.Length <> 0 Then
                    If StrEET_ADDR.Text.Length <> 0 Then
                        If CITY.Text.Length <> 0 Then
                            If StatesList.Text <> "Select Option" Then
                                If ZIP_CODE.Text.Length <> 0 Then
                                    If PHONE.Text.Length <> 0 Then
                                    Else
                                        Response.Write("<script language=""javascript"">alert('Please enter a Phone Number');</script>")
                                        FocusText = PHONE
                                    End If
                                Else
                                    Response.Write("<script language=""javascript"">alert('Please enter a Zip code');</script>")
                                    FocusText = ZIP_CODE
                                End If
                            Else
                                Response.Write("<script language=""javascript"">alert('Please select a State');</script>")
                                FocusDrop = StatesList
                            End If
                        Else
                            Response.Write("<script language=""javascript"">alert('Please enter a City');</script>")
                            FocusText = CITY
                    End If
                Else
                        Response.Write("<script language=""javascript"">alert('Please enter a Street');</script>")
                        FocusText = COMPANY_NAME
                End If
            Else
                    Response.Write("<script language=""javascript"">alert('Please enter a Comapny Name');</script>")
                    FocusText = FED_TAX_ID
            End If
        Else
                Response.Write("<script language=""javascript"">alert('Please enter a federal tax ID');</script>")
                FocusText = Password2
        End If
        Else
            Response.Write("<script language=""javascript"">alert('Both Passwords don't match. Please try again.');</script>")
            FocusText = Password2
        End If
    Else
        Response.Write("<script language=""javascript"">alert('Please enter a Password');</script>")
        FocusText = Password1
    End If

    'FocusText.Focus()
    'FocusDrop.Focus()
End Sub

UPDATE 我试图添加一个标签,但我必须点击两次提交按钮才能使焦点注册(我认为标签更改为页面重新加载的时间是关闭)不知道为什么?

Public Class SupplierUpdate
Inherits System.Web.UI.Page
Public Shared FocusTextv As TextBox
Public Shared myControl1 As Control

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'LocalUnionText.Text.Visible = False
    'Dim focusP As TextBox = Password2
    'focusP.Focus()
    myControl1 = FindControl(FocusText.Text)
    FocusTextv = myControl1

    FocusTextv.Focus()
..etc

Reponse.Write 变化

Response.Write("<script language=""javascript"">alert('Please enter a Owner Phone Extension');</script>")

FocusText.Text = "OWNERPHONEEXT"

已添加标签

<asp:Label ID="FocusText" runat="server" hidden="true">Password1</asp:Label>

`

您的 Response.Write 正在编写一个 JavaScript,它显示一个 alert,这是模态对话框。显示 alert 时,您无法获得任何焦点,但在您点击确定按钮后,焦点将起作用。

如果您在页面上使用标签而不是 alert 来显示您的消息,焦点将起作用。

注意:最好使用 ASP.Net 验证控件,因为它们提供了您要在此处实现的所有功能。

更新很难跟上你更新的变化,但我会努力的。

Page_Load 不应该有任何 Focus 的代码,因此请删除更新中现在拥有的那些树行:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'LocalUnionText.Text.Visible = False
    'Dim focusP As TextBox = Password2
    'focusP.Focus()
    'myControl1 = FindControl(FocusText.Text)
    'FocusTextv = myControl1
    'FocusTextv.Focus()
..etc

然后在 Register_Click 中,将每个 Response.Write 替换为 FocusText.Text = "..."。例如,更改:

Response.Write("<script language=""javascript"">alert('Please enter a Phone Number');</script>")

至:

FocusText.Text ="Please enter a Phone Number"

您的新 Label 的 ID 与您的其中一个变量的名称冲突,因此请将其更改为其他名称。您也不需要隐藏它,只需将其留空(没有文字)。示例:

<asp:Label ID="ErrorMessage" runat="server"></asp:Label>

并且在 Register_Click 的底部:

'FocusText.Focus()

我将 Response.Write 部分更改为如下所示(添加到我的更新中):

Response.Write("<script language=""javascript"">alert('Please enter a Owner Phone Extension');</script>")

FocusText.Text = "OWNERPHONEEXT"
myControl1 = FindControl(FocusText.Text)

FocusTextV = myControl1
FocusTextV.Focus()