Solved

TextChange event eats up button click in ASP.NET

Posted on 2004-08-22
8
2,311 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now