Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
C#
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Focus();
    }


or
C#
protected void Page_PreRender(object sender, EventArgs e)
    {
        TextBox2.Focus();
       
    }

or
C#
protected void Page_Load(object sender, EventArgs e)
    {
        Page.SetFocus(TextBox2);
    }



C#
protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.SetFocus(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!
Posted

This code is working fine on page load and cursor is also blinking

C#
TextBox1.Focus();
 
Share this answer
 
Comments
[no name] 28-Jan-13 13:54pm    
That is why I wrote textBox2
and textbox1 should be read-only.
[no name] 28-Jan-13 14:13pm    
Read the question carefully and then try to answer
bye
Try to use javascript
C#
window.onload = function () {
           document.getElementById('<%Response.Write(TextBox2.ClientID); %>').focus();
       }
 
Share this answer
 
Comments
[no name] 28-Jan-13 14:12pm    
thank you so much
Hi,

Try this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
       Page.Form.DefaultFocus = TextBox2.ClientID;
    }
 
Share this answer
 
Comments
[no name] 27-Jan-13 13:18pm    
Thanks for your reply.
But sir
Its not working, the same problem mentioned in my question persist.
Please help in any other way.
thanks
Thomas Daniels 27-Jan-13 13:36pm    
It's working fine for me.
[no name] 27-Jan-13 13:53pm    
no sir
in my case cursor is not blinking.
please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900