Javascript onfocus not working

The onfocus event is not firing for a textbox when the code is written as below:
txtEmployeeID.Attributes.Add("onfocus", "this.blur();")
When the same code is written for other events as:
txtEmployeeID.Attributes.Add("onclick", "this.blur();")
txtEmployeeID.Attributes.Add("onkeypress", "this.blur();")
txtEmployeeID.Attributes.Add("onblur", "alert('Blur event fired');")
it works
Does anyone know the reason why this strange behaviour with the onfocus event?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Munawar HussainPrincipal Software EngineerCommented:
this might help you in finding out the issue.
here onfocu and onBlur are discussed in detail.

Munawar HussainPrincipal Software EngineerCommented:
both are working fine
here is working code for that..

//class file
protected void Page_Load(object sender, EventArgs e)
        TextBox1.Attributes.Add("onfocus", "TestFocus(this)");
        TextBox1.Attributes.Add("onblur", "TestBlur(this)");  

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <script language ="javascript">
    function TestFocus(myControl)
    function TestBlur(myControl)
    //on leave control
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox2" runat="server">bb</asp:TextBox>
        <asp:TextBox ID="TextBox1" runat="server">aa</asp:TextBox></div>

// when a control get focus then onfocus event is called and when a control leaves focus then the blur event is called.. according to that the above example is working perfectly.
Munawar HussainPrincipal Software EngineerCommented:
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

psid23Author Commented:
Thanks needo_jee but I am aware of all the solutions you have.

The problem is that all events are firing except the onfocus event for my code.

I am not able to find out why only the onfocus event is not working.

Can we have an alternate solution to make the textbox read-only using javascript?

There is a certain restriction by virtue of which I cannot directly make the textbox readonly and I need to do this via javascript only.
Munawar HussainPrincipal Software EngineerCommented:
you may use this to make it readonly

Munawar HussainPrincipal Software EngineerCommented:
I can explain pros and cons of that if required .. but for your requiremets to make a textbox readonly above comments would work fine.
psid23Author Commented:
thanks needo_jee
bit I want to know how the textbox can be made readonly without setting its readonly property but using javascript
Munawar HussainPrincipal Software EngineerCommented:
well with JS this line or the one you wrote in your question seems working perfectly to make a textbox readonly

 TextBox1.Attributes.Add("onfocus", "this.blur();")

also, you are using IE 6 there should be no problem .. thought there is with some of the browsers version ..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
psid23Author Commented:
Thanks needo_jee
Your solutions were right to the point
Howevere, the problem was with a javascript menu used in the site that was causing the problem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.