We help IT Professionals succeed at work.

JQuery Detecting page post back

Mymail cpn
Mymail cpn asked
on
522 Views
Last Modified: 2017-03-22
Hello,

How can I detect the page post back in my JQuery.

I am using Visual Studio 2015 with JQuery.

Scenario: I am populating my dropdown on the page using JQuery. But it needs to be done only once.
At present it is doing on every post back. So I can I check the page post back with JQuery?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
You can check to see if the drop down is populated using jQuery.

if ($('#myDropDownList option').length === 0) {
    //load options
}

Open in new window


Or, set a hidden field on postback and check if that field has a value in jQuery before you populate the drop down.

Author

Commented:
Hello, I would like to verify the length as you suggested. But the syntax is throwing error.

I am trying to select my dropdown as

 $('[id*=DDList1]').append("CityName");

How do I check for length of my Dropdown?
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
At present it is doing on every post back. So I can I check the page post back with JQuery?
Can you show us your code?
I am confused by what you mean it is populating on every post-back? Is the jQuery in the post-back code? Why would it run on each post back.

Obvious answer is move your jQuery outside of whatever is being refreshed by the post back and then you don't have to detect anything.

Author

Commented:
Thank you very much.

Author

Commented:
Your help is greatly appreciate.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.