Solved

TextChange event eats up button click in ASP.NET

Posted on 2004-08-22
8
2,538 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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