# Other Languages > jQuery >  JQUery Must Not Affect Code Behind

## aNubies

Hi guys I want to know if its possible to execute my code behind (VB code) even the jquery execute first.
To give you a more details on what I am saying, this is what i have



```
$('#first_div').show();
$('#second_div').hide();

$('#button1').click(function() {
    $('#first_div').slidetoggle('slow');
    $('#second_div').hide();

    $(this).slideToggle('active');
    return false;
});
$('#button2').click(function() {
    $('#second_div').slidetoggle('slow');
    $('#first_div').hide();

    $(this).slideToggle('active');
    return false;
});
```

with those code my code behind (VB code) doesn't execute anymore. If turn it into return true it will execute however the jquery will not work.

Please help me understand this JQuery  :Smilie: . Thank you

----------


## szlamany

Do you realize that the code behind you refer to is actually posting a page back to you?  At least a partial post back...

And the jQuery is running JAVASCRIPT on the page - in the browser window - you could unplug the network cable and that JAVASCRIPT code would run - calling those jQuery methods and such.

So - you now realize the disconnect in that.  One is page resident - the other runs on the server and pumps a new page back to you.

What are you wanting the code behind to give you anyway?

----------


## techgnome

ah... I think I know what's going on... the js posted are click events for a couple of buttons... by returning false, it cancels out the event... something has gone wrong and processing stops. That includes the postback. By returnign True, then the processing of the event was sucessful and things can continue...which should include the postback. however... depending on how that  postback happens, as szlamany noted, a new page is rendered and sent back, causing the screen to refresh... and so it gives the appearance that the js isn't working, when in fact it very may well be running but in the context of a new page. local js code will run first... that's a given... only after successful run of that will the page postback to the code behind, which then runs on the server.

-tg

----------


## szlamany

The behavior of return false and return true - as decribed by TG - is exactly what is going on - I thought you had already discovered that fact.

Are you using a browser debugger - like Firebug (used with Firefox)?  That would help you see when the post backs are happening and allow you to step through your JAVASCRIPT script file line by line and then you can watch the jQuery be consumed and then blown away by the post back (I'm guessing this bit - but that's probably what's happening).

Again - why are you wanting to affect the "page" locally and also expecting to do a post back?  Do you readily mix ASP.Net button events with jQuery?  What are you trying to accomplish??

----------


## aNubies

Sorry for the late reply and thank you for your responses. Base on what you've said guys give me some enlighten.
As for Sir szlamany yeah I mix up ASP.NET button events with JQuery, for JQuery the animation and for the ASP.NET the functionality.

As you can see the JQuery simply adds the slide effect on to the div when the button pressed and with that there should be another function that will fire in the ASP.Net codebehind.
Both of your insights that is correct that if I turn the "return false" the animation will take effect but not the event in code behind however by turning it true that animation will take effect one only but the event code behind will fire.

So that's basically what I'm trying to accomplish by combining the JQuery and Codebehind, so I'm hoping that you can help to understand if I can combine them or not, the difference of turning false and true.

Thank you in advance.

[PS]
To Sir szlamany to be more precise on what my CodeBehind will do is that after the pressed it will populate all the data in my gridview.

----------


## szlamany

I don't see how you can use jQuery and a postback together in this situation.

The jQuery will move the slider - and you simply will not get a chance to see it (unless stepping through the code!!!)...

...because the page will re-load from the server (unless you are doing a careful partial postback in some kind of update panel)...

Have you considered the more standard jQuery approach which is to use an AJAX call to get data and load the page DOM yourself in the browser??

Look at the SLICKGRID control - I left ASP.NET for this jQuery GRID two years ago and never looked back!!

----------


## aNubies

I don't know if this has a connection though I will give it anyway. If I remember correctly I just recently use JQuery Masking together with my DropDownlist CodeBehind event.
What I've done is that for every selection of data on my DropDownlist it will change the masking of my textbox.

i.e

Dropdownlist data:
Cellphone number
landline number

My CodeBehind dropdownlist event is the SelectedIndexChanged. So if I select the cellphone number it will put the masking of cellphone number in my textbox and its the same with the landline number masking. So Hypothetically its the same with my current situation, right?

----------


## szlamany

Have you ever debugged a page with FIREBUG - so you could see the posts and gets and such being done by ASP.NET?  All that activity - SELECTEDINDEXCHANGE and all - is talking to the server and sending page data back and forth.

Your jQuery stuff is all sitting on the browser running locally - nothing new is being brought from the server - you are simply affecting the page as it's being displayed in the browser.

You are asking us to explain to you how those two do not communicate and how they might interfere with each other.

Technical explanation...

The RETURN FALSE causes the CLICK event to NO LONGER bubble-up to other listeners for that event - thus cancelling the ASP.NET listener that does it's job with that click.

When you remove that the click event is handled by the ASP.NET listener which - if you do not see the jQuery "slide-effect" - must be causing the page to re-draw so you never see the "visual" of the "slide-effect".

One way to prove this to yourself is to step through the code with FIREBUG and see the slide-effect happen and then step once or twice again and see the page re-draw.

----------

