Solved

TextChange event eats up button click in ASP.NET

Posted on 2004-08-22
8
2,418 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing Flash from Website 6 63
Tool to email me when a website changes 29 128
Create and populate iFrame onClick of a button? 1 77
Facebook React and SyntheticEvents 6 64
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 …
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

786 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