ASP.NET中的TextBox焦点问题 [英] TextBox Focus Issue in ASP.NET

查看:89
本文介绍了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();


这篇关于ASP.NET中的TextBox焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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