Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#3.5
<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.
 
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 28-Jan-13 20:10pm
mu-kul333
v2
Comments
MT_ at 29-Jan-13 1:58am
   
What are doing in change function called on onfocus ?
mu-kul at 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)
Tadit Dash at 29-Jan-13 3:02am
   
Can you please share the Change() function?
mu-kul at 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";
}
}
Tadit Dash at 29-Jan-13 3:55am
   
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)?
mu-kul at 29-Jan-13 4:07am
   
This is first Textbox
<asp:TextBox ID="txtAccountNoCredit" runat="server" CssClass="Text" AutoPostBack="true" MaxLength="10" onblur="Change(this, event)" onfocus="Change(this, event)"
OnTextChanged="txtCustomerCodeCredit_OnTextChanged">

In this OnTextChanged event firing correctly and in that i'm setting focus to next Textbox(txt1)
The following is .aspx.cs
protected void txtCustomerCodeCredit_OnTextChanged(object sender, EventArgs e)
{
txt1.focus();
}
Tadit Dash at 29-Jan-13 5:36am
   
Please check my answer.
mu-kul at 29-Jan-13 8:21am
   
I checked it.The Code which you sent to me work fine.
But in my problem it is not happening correctly.I think something else processing between first textbox lost focus and second textbox gain focus.One more thing when second textbox get focus it don't show color change but when i clicked elsewhere and again click in that tetxtbox it is showing backcolor changed to blue.
Thank you for your great help hope you will help me to find accurate solution.
Tadit Dash at 29-Jan-13 8:34am
   
This problem also working fine with my code, am I right?
Because, I tested just now in Firefox and IE, and found that the color is changing when second textbox gets focus.
 
Please do a thorough research on your code and mine and try to find the differences.
 
And try to debug the JavaScript codes using Firebug and server side code using break points. Then only, you can check the flow of page and execution of events.
 
Please reply with your findings.
 
Thanks,
Tadit
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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/[^]
$(document).ready(function()
{
$('#txt1').bind('blur focus', function() {
 alert("Worked");
});
});
 
Hope this helps
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Let me tell you in detail what and how I have tested.
 
Environment - Firefox, Visual Studio 2010.
 

Code -
1. aspx -
<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 -
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

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100