Results 1 to 14 of 14

Thread: [RESOLVED] call JQuery from ASP.net Button Click

  1. #1

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Resolved [RESOLVED] call JQuery from ASP.net Button Click

    hay there,
    i am new to JQuery,
    so i wounder if there is a way to call JQuery method after button click from server side event,

    here is what i really try to do,
    the user will click on the login Button, and i will check his login name and his user name on the server side,
    then i will call the JQuery function to execute slideToggle for a Div

    is there a way to do such a thing

    here is my JQuery function
    Code:
    function LoginError() {
    $(document).ready(function() {
            $("LoginButton").click(function() {
                $(".DivError").slideToggle("slow");
                return false;
            });
        });
    }
    Code:
    bool loginFlag = false;
    
    if(!loginFlag){
    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
                "startup", "<script type=\"text/javascript\">LoginError();</script>");
    }
    but there is nothing happened,
    i think i am missing something
    thanks alot.
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: call JQuery from ASP.net Button Click

    You can just run this on the client side:
    JavaScript Code:
    1. function LoginError() {
    2.     $(".DivError").slideToggle("slow");
    3. }
    and then call it from the server-side using ClientScript.RegisterClientScriptBlock().

    You don't want to bind it to the button click event because the button is calling the server-side code to authenticate the user. If you did that, the div would toggle every time. Also, returning "false" from a client script event handler (the button click event) will prevent the asp.net button postback from occurring to run the server-side click event.

    You also don't need the document.ready() here because the client-side function won't be called until the page/DOM has already loaded fully (which is what the document.ready waits for).
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: call JQuery from ASP.net Button Click

    ok, thanks tr333

    i used this now

    Code:
      protected void Button2_Click(object sender, EventArgs e)
        {        
            bool flag = true;
            if (flag)
            {
               this.ClientScript.RegisterClientScriptBlock(this.Page.GetType(),
               "Error", "$(document).ready(function() {$(\".flip\").click(function() {$(\".panel\").slideToggle(\"slow\");});});", true);
    
            }
        }
    i can see the div for less than 1/3 sec,
    this when i remove the return false;
    i can see the Div from the second click if the return false; exist
    so i don't actually know where is the problem
    Last edited by avrail; Apr 14th, 2011 at 02:25 AM.
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  4. #4
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: call JQuery from ASP.net Button Click

    If you remove the document.ready from the script you are sending from the server-side (remeber that DOM is already loaded when doing this, so document.ready is not required), then you would get:

    JavaScript Code:
    1. $('.flip').click(function() {
    2. $('.panel').slideToggle('slow');
    3. });

    Do you want to only have the panel toggle visibility when clicking the ".flip", and not immediately after sending this client-script from the server-side?

    Don't forget that this click event handler on ".flip" will only be added after the script is sent from the server-side button click event.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  5. #5

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: call JQuery from ASP.net Button Click

    hay tr333,
    thanks again
    the flip css class if the button
    Code:
    <asp:Button ID="Button2" Text="Show Refat and Hide Karim" runat="server" CssClass="flip" 
                                    onclick="Button2_Click" />
    what i need is when the user click the login button and after the check for the login on the server done
    i want show the Div immediately if the user login failed
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  6. #6
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: call JQuery from ASP.net Button Click

    You can remove CssClass="flip" from the asp:button, since this button is going to call the server-side button click event to handle the login validation. If the login validation fails inside this server-side event, then you just need to pass the script back in a RegisterClientScriptBlock() to show the div.

    JavaScript Code:
    1. $('.panel').slideToggle('slow');

    You don't need any client-side click event handlers, because the validation is all being done on the server-side.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  7. #7

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: call JQuery from ASP.net Button Click

    you are right tr333
    your last post solved my problem,
    i think i need to read more before i use JQuery
    thanks a lot,
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  8. #8
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    The best way to learn is practice
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  9. #9

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    you are right
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    Out of curiousity tr333, do you have any links for where you picked up jQuery? It has been on my list of things to pick up for a while, but not really got round to it yet

    Gary

  11. #11

    Thread Starter
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    i begin with w3schools, and i think it is a good beginning for me
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    That used to be the same for me as well, although I have recently heard some criticism for w3schools, and htmldog:

    http://htmldog.com/

    Is a better source for HTML and CSS related information, so I am curious as to whether the jQuery information at w3schools is any good?!?

    Gary

  13. #13
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    For jQuery I've just used the jQuery website and Google, browsing through the jQuery API reference to see what it can do. Also useful is jQueryUI and the upcoming jQuery Mobile.

    For generic HTML/CSS/JavaScript reference I always use Mozilla Developer Network. Another great reference is Dev.Opera which has some great articles on efficient JavaScript development, among other things.

    I started learning jQuery when I needed to do a deep-copy merge of two objects and found that prototype.js (which I was using at the time) could only do shallow-copy merging.
    Last edited by tr333; Apr 25th, 2011 at 04:58 AM.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] call JQuery from ASP.net Button Click

    Thanks for the links!

    Gary

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