 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 27th, 2009, 04:12 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
TextChanged Event Not Firing
Hi,
Whenever I enter a value in the textbox i want to get the values related to that of text value.without button click i should raise the event...
Code:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongDateString() + ":No Change";
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongDateString() + ":CHANGED";
}
HTML Code:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
whenever i enter a value in the textbox and press Enter button then the code is coming....i should not get like that without entering the enter button i should get the values.On text Focus only i should get the values....
|
|

August 27th, 2009, 05:01 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're setting the text box value after each post back as well and thus TextChanged doesn't fire. Wrap your code in Page_Load in a !PostBack block:
Quote:
if (!Page.IsPostBack)
{
Label1.Text = DateTime.Now.ToLongDateString() + ":No Change";
}
|
Hope this helps,
Imar
|
|

September 16th, 2009, 06:20 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Text changed event will be fire on going out of the text box(onBlur in client)
|
|

September 16th, 2009, 06:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Text changed event will be fire on going out of the text box(onBlur in client)
|
This is absolutely not true. TextChanged fires at the server after a PostBack when the text has been changed somehow compared to the initial load of the page. It is not, in any way, related to JavaScript's onblur or any other client event. You can have TextChanged fire on browsers that have JavaScript disabled or that don't support JvaaScript.
Simply put: TextChanged fires when the text for a control has changed at the client, irrespective of the actual method used to change that text (a user, client side JavaScript etc).
Cheers,
Imar
|
|

September 16th, 2009, 09:29 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Quote:
Originally Posted by Imar
This is absolutely not true. TextChanged fires at the server after a PostBack when the text has been changed somehow compared to the initial load of the page. It is not, in any way, related to JavaScript's onblur or any other client event. You can have TextChanged fire on browsers that have JavaScript disabled or that don't support JvaaScript.
Simply put: TextChanged fires when the text for a control has changed at the client, irrespective of the actual method used to change that text (a user, client side JavaScript etc).
Cheers,
Imar
|
the textchanged event fires only after you are leaving away the text box by taping a tab or, clicking on another control in case of AutoPostBack=true for the respective control.
it is hard for asp.net engine to do postback for every letter change in the textbox. may be the naming of the event is confusing here. Thats the reason i have put onBlur in the brackets for the understanding purpose. I have verified this with vb.net and c#.net and it fires only on leaving the textbox(same as onBlur)
NOTE: the textbox control by design will NOT fire event( TextChanged) for every keypress.
Last edited by pons_saravanan; September 16th, 2009 at 10:36 PM..
|
|

September 17th, 2009, 03:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
the textchanged event fires only after you are leaving away the text box by taping a tab or, clicking on another control in case of AutoPostBack=true for the respective control.
|
No, no, no. You're mixing things up.
What you are describing here is AutoPostBack which causes the TextBox to submit the form to the server once you leave the TextBox control. This is indeed where onblur comes it. As soon as you trigger a JavaScript onblur for a TextBox with AutoPostBack to true it does a postback to the server. And you're right: it only does that for the entire text, not for each letter.
But all of this has nothing to do with TextChanged. Like I said, TextChanged fires at the server, after a post back, when the text in the control has changed. Not for every letter indeed, but once for every postback. It does that after a postback which can be triggered by a plain button (no JavaScript involved) or because you set AutoPostBack to true on the textbox. However, the use of JaavScript to handle onblur and submit to the server is ooure coincidence; it's not related to TextChanged; it's just a means to submit back to the server.
You can easily witness the behavior by creating a form with a Button, a TextBox (without AutoPostBack) and a handler for TextChanged. You'll see that no JavaScipt is needed or used. You enter some text, hit the button (which causes the TextBox to loose its focus, causing an onblur but NOT causing a PostBack) and then the form is submitted to the server where TextChanged is fired.
So, this is all a server side solution. Onblur has nothing to do with it, although you can use it to force a postback to the server.
Hope this clariefies things and clears up your confusion.
Cheers,
Imar
|
|

September 17th, 2009, 03:30 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Quote:
Originally Posted by Imar
No, no, no. You're mixing things up.
What you are describing here is AutoPostBack which causes the TextBox to submit the form to the server once you leave the TextBox control. This is indeed where onblur comes it. As soon as you trigger a JavaScript onblur for a TextBox with AutoPostBack to true it does a postback to the server. And you're right: it only does that for the entire text, not for each letter.
But all of this has nothing to do with TextChanged. Like I said, TextChanged fires at the server, after a post back, when the text in the control has changed. Not for every letter indeed, but once for every postback. It does that after a postback which can be triggered by a plain button (no JavaScript involved) or because you set AutoPostBack to true on the textbox. However, the use of JaavScript to handle onblur and submit to the server is ooure coincidence; it's not related to TextChanged; it's just a means to submit back to the server.
You can easily witness the behavior by creating a form with a Button, a TextBox (without AutoPostBack) and a handler for TextChanged. You'll see that no JavaScipt is needed or used. You enter some text, hit the button (which causes the TextBox to loose its focus, causing an onblur but NOT causing a PostBack) and then the form is submitted to the server where TextChanged is fired.
So, this is all a server side solution. Onblur has nothing to do with it, although you can use it to force a postback to the server.
Hope this clariefies things and clears up your confusion.
Cheers,
Imar
|
i never said anywhere that javascript is doing postback, i said for understanding purposes i refered onBlur nothing more than that. Can you please re read the original requirement asked by the thread starter and re read your post again. You will know what i meant.
|
|

September 17th, 2009, 03:41 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You said this initially:
Quote:
|
Text changed event will be fire on going out of the text box(onBlur in client)
|
This is simply not true. TextChanged does NOT fire when going out of the text box. It fires, at the server, when the text has changed compared to the initial load of the page. You can have a TextChanged fired without ever entering (and thus without ever going out of) the text box. So, rather than explaining things, I think your original statement just confuses matters....
Cheers,
Imar
|
|

September 17th, 2009, 03:58 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Quote:
Originally Posted by Imar
You said this initially:This is simply not true. TextChanged does NOT fire when going out of the text box. It fires, at the server, when the text has changed compared to the initial load of the page. You can have a TextChanged fired without ever entering (and thus without ever going out of) the text box. So, rather than explaining things, I think your original statement just confuses matters....
Cheers,
Imar
|
can you please check your answer respect to the question asked by the thread starter?
Is it anywhere relevant to the question? He is not asking how the event is working. he is asking why the event is not firing without pressing enter .
if you still not able understand his queries i am giving up here. and the readers of this forums will understand what i am talking about.
|
|
 |