ASP.NET中的TextBox焦点问题
问题描述:
我正在ASP.NET中开发一个简单的Web应用程序。
我的第一个TextBox设置为ReadOnly。
所以我想要专注于我的第二个TextBox即TextBox2。
我尝试使用以下代码。
I am developing a Simple Web Application in ASP.NET.
My First TextBox is set as ReadOnly.
So I want to focus on my second TextBox i.e. TextBox2.
I tried with the following codes.
protected void Page_Load(object sender, EventArgs e)
{
TextBox2.Focus();
}
或
or
protected void Page_PreRender(object sender, EventArgs e)
{
TextBox2.Focus();
}
或
or
protected void Page_Load(object sender, EventArgs e)
{
Page.SetFocus(TextBox2);
}
protected void Page_PreRender(object sender, EventArgs e)
{
Page.SetFocus(TextBox2);
}
但没有任何代码正常工作。
这些代码将光标置于TextBox2中。但是光标没有闪烁。
所以,在我用鼠标移动指针并在文本框中单击之前,我无法在文本框中书写。
我需要光标闪烁并且能够在页面加载时写入。
请建议!
but none of the code is working.
Cursor placed in TextBox2 by those code. but the cursor is not blinking.
So, I can''t write in the textbox until I use my mouse to move the pointer and click there in textbox.
I need the cursor to be blink and be able to write at the time of page load.
Please suggest!
答
尝试使用javascript
Try to use javascript
window.onload = function () {
document.getElementById('<%Response.Write(TextBox2.ClientID); %>').focus();
}
试试这个:
Hi,
Try this:
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.DefaultFocus = TextBox2.ClientID;
}
此代码在页面加载时工作正常,光标也闪烁
This code is working fine on page load and cursor is also blinking
TextBox1.Focus();