Solved

TextChange event eats up button click in ASP.NET

Posted on 2004-08-22
8
2,463 Views
Last Modified: 2008-01-09
I have a page with couple of textboxes and 2 buttons. One of textboxes has Autopostback=true. If textbox with autopostback=true is empty and I type something into it and click, using my mouse, on any of the buttons all events fire as expected Page_Load, TextChanged, Button.Click. However if textbox is not empty (already contains some text) and I just change this text and click on the button I get only Page_Load and Textchanged fired, no Button.Click event. Does anyone knows what is the problem and how to workaround it?

Thanks.
0
Comment
Question by:DAE012
  • 4
  • 2
8 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 11867330
As far as my understanding goes the Page_Load occurs for all postbacks and only one event can cause a postback from the client. It must either be a textchanged or a Button Click. One a character is keyed in it will cause a postback and when the button is clicked it will cause a postback.
Correct me if i am wrong.
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 11867415
Well the Workaround. Set a HiddenVariable when ever you want a event to occur in the server and then check for the variable in the Page_load, call the method manually
Lets assume you on click of the button/ text changed event in JAVASCRIPT you set the hiddenvariable hdnCallTextChangedAlso = true

In Page Load Check if hdnCallTextChangedAlso.value == true, Call the method manually as
TextBox1_TextChanged(sender,e)

hdnCallTextChangedAlso.value = false (Remember to set the hdnCallTextChangedAlso as false after calling the method)
0
 
LVL 1

Author Comment

by:DAE012
ID: 11877346
Dear surajguptha,

Thanks for your comments. I think you did not quite understand the issue, I'll try again. I have a page with a set of texboxes and couple of buttons. One of textboxes has AutoPostback=true and has TextChanged event handler associated with it. This is done so when user leaves this field (not when he presses a key as you suggested) page should perform a postback, so I do not want to wait for some postback to happen in order to check if textbox changed, I need postback when textbox field loses focus.
 Your first comment is not quite right either, it is not only one or another event (key or button) it is supposed to be chain of events and if you read my question I mentioned that if textbox is empty and user puts something in than whole chain of events happens as expected Page_Load -> TextChanged -> Click, it is not working only if textbox already contains some text before user types something in and clicks with mouse on the button.

Regards.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Accepted Solution

by:
surajguptha earned 200 total points
ID: 11877511
Requirement:(Please correct me if i am wrong)
             On Lost Focus of the TextBox the Page must be posted back

Game Plan
------------
0. This way there is no need of a TextChanged Event and an unnecessary postback everytime
1. When ever there is a blur(lost focus) on the Textbox the Javascript manually submits the page.
2. This manual submission cannot be caught in the server
3. So a hidden variable is used to indicate that the LostFocus event caused the postback
4. In the Page_Load you can check if the hidden variable is set and do the necessary processing by calling a function
     
Coding
--------
Ok you can do that in javascript by
1) Adding a Javascript event ONBLUR to the textbox

<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 104px" runat="server" onblur="SubmitPage();"></asp:TextBox>
                                 ---------------------------

2) Define a Hidden Variable
<input type=hidden name="hdnFlag" runat=server value="No" id="hdnFlag">

3) Define the SubmitPage() Method as

<script type="text/javascript">
                      function SubmitPage()
           {
                document.all.hdnFlag.value="Yes";
                Form1.submit();                  
           }
</script>

4)Catch the variable in Page_load and call function

                                private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  if (hdnFlag.Value.Trim()=="Yes")
                  {
                        Calculate(TextBox1.Text);
                  }
            }


0
 
LVL 21

Expert Comment

by:surajguptha
ID: 11877831
If you want to make sure that the postback does not occur when the textbox is empty you can do that too in Javascript. Let me know if you have that requirement too
0
 
LVL 1

Author Comment

by:DAE012
ID: 11987382
Dear surajguptha,

Thanks for your comments. I apologize for not responding earlier. Your solution plan should work, but I would prefer avoid heavy coding in  Javascript for each case. It is Ok if you dealing with one-two pages, but I need to add this to quite a substantial number of pages/fields (more than a hundred pages and most of them contain 2-5 autopostback textboxes).
I have a feeling that this is some kind of bug in VS (see more explanation above - it is working fine if field is empty) and there should be either some patch or simplier workaround than rewriting this functionality manually.
Appreciate your effort anyway.

Regards.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Field that has HTML displays the code instead of the text in html 16 92
Form submit issue 11 86
Where do I learn ES6? 3 78
The Best Website Creation Tools 14 73
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question