Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:TextBox ID="txt1" runat="server" CssClass="Text" AutoPostBack="true" MaxLength="10" OnTextChanged="txt1_OnTextChanged" onblur="Change(this, event)" onfocus="Change(this, event)">

</asp:TextBox>


This is my Textbox code when i move focus from previous Textbox To this one (txt1) and input some value then it don't work(don't fire event) BUT if i click on form anywhere and then i input value then it will work fine.

C#
protected void txt1_OnTextChanged(object sender, EventArgs e)
{
   Some line of code
   ------------
   ------------
}

This above code in aspx.cs file

Plz help me on this
thanks.
Posted
v2
Comments
MT_ 29-Jan-13 1:58am    
What are doing in change function called on onfocus ?
mu-kul 29-Jan-13 2:08am    
It is just to highlight textbox i'm just changing color of textbox as soon as it gets focus.I tried to remove it but it shows no effect (problem as it is)
Can you please share the Change() function?
mu-kul 29-Jan-13 3:29am    
function Change(obj, evt)
{
if(evt.type=="focus")
{
obj.style.borderColor="blue";
obj.style.backgroundColor="Aqua";
}
else if(evt.type=="blur")
{
obj.style.borderColor="black";
obj.style.backgroundColor="white";
}
}
Seems like working in Firefox with the current code.
Can you provide the other textbox markup (from which you are coming to this textbox and it is not working)?

Use jquery.You can bind multiple events on one control.Please make sure that you must put jquery pluging for implementing jQuery.You can get jQuery plugin from here http://jquery.com/[^]
JavaScript
$(document).ready(function()
{
$('#txt1').bind('blur focus', function() {
 alert("Worked");
});
});


Hope this helps
 
Share this answer
 
v2
Let me tell you in detail what and how I have tested.

Environment - Firefox, Visual Studio 2010.


Code -
1. aspx -
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function Change(obj, evt) {
            if (evt.type == "focus") {
                obj.style.borderColor = "blue";
                obj.style.backgroundColor = "Aqua";
            }
            else if (evt.type == "blur") {
                obj.style.borderColor = "black";
                obj.style.backgroundColor = "white";
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox    ID="txtAccountNoCredit"
                        runat="server"
                        CssClass="Text"
                        AutoPostBack="true"
                        MaxLength="10"
                        onblur="Change(this, event)"
                        onfocus="Change(this, event)"
                        OnTextChanged="txtCustomerCodeCredit_OnTextChanged">
        </asp:TextBox>

        <asp:TextBox    ID="txt1"
                        runat="server"
                        CssClass="Text"
                        AutoPostBack="true"
                        MaxLength="10"
                        OnTextChanged="txt1_OnTextChanged"
                        onblur="Change(this, event)"
                        onfocus="Change(this, event)">
        </asp:TextBox>
    </div>
    </form>
</body>
</html>

2. aspx.cs -
C#
protected void txt1_OnTextChanged(object sender, EventArgs e)
{
    Response.Write("Text Changed Called.");
}
protected void txtCustomerCodeCredit_OnTextChanged(object sender, EventArgs e)
{
    txt1.Focus();
}



Test Cases -
1. Focus out from first textbox
-> Moved focus from textbox txtAccountNoCredit (Pressed tab key).
-> txtCustomerCodeCredit_OnTextChanged is getting called.
-> Now, second textbox txt1 is focused.
-> Entered some text and pressed tab again (or clicked outside the textbox) to focus out from this textbox.
-> txt1_OnTextChanged is getting called and "Text Changed Called." is getting printed.

2. Clicking on form and focusing on the textbox
-> Clicked anywhere on the form.
-> Then clicked on the second textbox txt1.
-> Entered some text and pressed tab again (or clicked outside the textbox) to focus out from this textbox.
-> txt1_OnTextChanged is getting called and "Text Changed Called." is getting printed.


Conclusion -
So, this is working fine.


Note -
Let me tell you something.

Refer - TextBox.TextChanged Event[^], which says...

The TextChanged event is raised when the content of the text box changes between posts to the server.
For instance -

1. If you just focus into the textbox and then focus out immediately without entering anything, then the OnTextChanged will not be called.
So, make sure you change something with the content of the textbox before focusing out.

2. You have some text "abcd" in the textbox already. You focused in and changed nothing, but focusing out, then also the OnTextChanged will not be called.

Please test and check thoroughly, then let me know.

Thanks,
Tadit
 
Share this answer
 

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