Results 1 to 8 of 8

Thread: JQUery Must Not Affect Code Behind

  1. #1

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    JQUery Must Not Affect Code Behind

    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

    Code:
    $('#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 . Thank you
    Last edited by aNubies; Apr 26th, 2013 at 03:41 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQUery Must Not Affect Code Behind

    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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: JQUery Must Not Affect Code Behind

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQUery Must Not Affect Code Behind

    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??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: JQUery Must Not Affect Code Behind

    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.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQUery Must Not Affect Code Behind

    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!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: JQUery Must Not Affect Code Behind

    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?

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQUery Must Not Affect Code Behind

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width