Solved

TextChange event eats up button click in ASP.NET

Posted on 2004-08-22
8
2,370 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to find duplicate message in database 7 29
Forbidden errors 5 123
JQuery tracking event. 3 57
Problem to page 4 72
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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 …

910 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

22 Experts available now in Live!

Get 1:1 Help Now