根据我的编码,我想要关注标记消息框的文本框。
如果我在FocusText = Password2
Response.Write
工作后Response.Write
Response.Write
。
因此,我尝试为FocusText创建一个全局变量(并删除Password1
之后的行),但是" Page load"无论如何,Response.Write
的声明似乎都会覆盖它。我该如何纠正?
根据我的要求:
页面加载时,光标将重点放在Password1上。
用户点击提交按钮后,不会填写Password2文本框。代码将标记Company Name
,然后将光标聚焦回Password2。
- 如果用户没有填写federal 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
等等,则会出现这种情况....
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
更新我尝试添加标签,但是我必须单击两次提交按钮才能注册焦点(我认为当页面重新加载时标签更改为的时间)不确定为什么?
Response.Write("<script language=""javascript"">alert('Please enter a Owner Phone Extension');</script>")
FocusText.Text = "OWNERPHONEEXT"
Reponse.Write Change
<asp:Label ID="FocusText" runat="server" hidden="true">Password1</asp:Label>
已添加标签
save()
`
答案 0 :(得分:1)
您的Response.Write
正在编写一个显示alert
的JavaScript,这是一个模态对话框。显示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()
答案 1 :(得分:0)
我将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()