Basic JavaScript Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Merwitz

    Basic JavaScript Question

    hi

    I am using Javascript to do my validation
    This is so i can display a loading message after a form is submitted.

    Now, currently i do so like this.

    I have a button, textbox and label like so:

    <asp:textbox id="tb" runat="server"/>
    <asp:Button id="btn" runat="server" text="Submit" />
    <asp:Label id="lbl" runat="server" />

    I then have a javascript function like so:

    function validate()
    {
    if(document.get ElementById('tb ').value == '')
    {
    document.getEle mentById('lbl') .innerHTML = "please fill in
    textbox";
    return false;
    }
    else
    {
    document.getEle mentById('lbl') .innerHTML = "Loading... ";
    return true;
    }
    }

    And i attach this to the button in the code behind like so:

    btn.Attributes. Add("onclick", "JavaScript : return validate();");


    now, this works perfectly.
    but what i would like to do, is disable the button if the value returned is
    true.
    Like this:
    else
    {
    document.getEle mentById('lbl') .innerHTML = "Loading... ";
    document.getEle mentbyId('btn') .enables = false;
    return true;
    }

    But when i try this, the form will not submit.
    How can i achieve what i'm trying to do here?

    Submit a form only if validated, and disable the button if so?

    TIA

    Grant


  • slagomite

    #2
    Re: Basic JavaScript Question

    Hrm, interesting. I was able to replicate this -- apparently if the
    calling object gets disabled, the browser cancels any subsequent
    actions once control leaves the validate() function.

    I was able to work around this by explicitly calling the
    "__doPostBack() " method in the validate() function -- making an
    explicit call to this should be identical to the form itself being
    posted (if you create a LinkButton control and look at the page source,
    you'll see that ASP.NET just does the same thing -- the LinkButton's
    "onclick" event makes an explicit call to __doPostBack(), with its ID
    as the argument).

    So try this (note you'll have to create this script from the
    server-side -- i.e., in the Page.PreRender handler -- in order to get
    the reference to the postback method):

    StringBuilder sb = new StringBuilder() ;
    sb.Append("<scr ipt language='javas cript'>\r\n");
    sb.Append("func tion validate()\r\n" );
    sb.Append("{\r\ n");
    sb.Append(" if(document.get ElementById('tb ').value == '')\r\n");
    sb.Append(" {\r\n");
    sb.Append(" document.getEle mentById('lbl') .innerHTML = 'Try
    again...';\r\n" );
    sb.Append(" return false;\r\n");
    sb.Append(" }\r\n");
    sb.Append(" else\r\n");
    sb.Append(" {\r\n");
    sb.Append(" document.getEle mentById('lbl') .innerHTML =
    'Loading...';\r \n");
    sb.Append(" ");
    sb.Append(this. GetPostBackEven tReference(btn) ; sb.Append(";\r\ n");
    sb.Append(" return true;\r\n");
    sb.Append(" }\r\n");
    sb.Append("}\r\ n");
    sb.Append("</script>\r\n");
    this.RegisterCl ientScriptBlock ("_PageScripts" , sb.ToString());
    btn.Attributes["onclick"] = "return validate();";

    HTH,
    Luke

    Comment

    • S. Justin Gengo [MCP]

      #3
      Re: Basic JavaScript Question

      Grant,

      I have a free javascript component on my website (comes with all source
      code) that has as one of its methods a disable submit script. The code
      hasn't been updated for .net 2.0 yet, but it's an easy conversion from 1.1
      to 2.0. I just haven't had time to do it yet. If you take that code and add
      your javascript for displaying your message to it you'll have what you need.
      Another nice feature of the script is that before it disables the submit
      button it first calls the microsoft javascripts that handle page validation
      so that the button won't be disabled if the page isn't valid.

      If you have any questions about converting the script or adding your bit of
      javascript for your load message to it feel free to email me.

      You may see a demo of the script (#3) and download the entire component as a
      ..net 1.1 project from here:
      http://www.aboutfortunate.com?page=javascriptdemo


      --
      Sincerely,

      S. Justin Gengo, MCP
      Web Developer / Programmer



      "Out of chaos comes order."
      Nietzsche
      "Grant Merwitz" <grant@workshar e.com> wrote in message
      news:eneMFgYLGH A.2828@TK2MSFTN GP12.phx.gbl...[color=blue]
      > hi
      >
      > I am using Javascript to do my validation
      > This is so i can display a loading message after a form is submitted.
      >
      > Now, currently i do so like this.
      >
      > I have a button, textbox and label like so:
      >
      > <asp:textbox id="tb" runat="server"/>
      > <asp:Button id="btn" runat="server" text="Submit" />
      > <asp:Label id="lbl" runat="server" />
      >
      > I then have a javascript function like so:
      >
      > function validate()
      > {
      > if(document.get ElementById('tb ').value == '')
      > {
      > document.getEle mentById('lbl') .innerHTML = "please fill in
      > textbox";
      > return false;
      > }
      > else
      > {
      > document.getEle mentById('lbl') .innerHTML = "Loading... ";
      > return true;
      > }
      > }
      >
      > And i attach this to the button in the code behind like so:
      >
      > btn.Attributes. Add("onclick", "JavaScript : return validate();");
      >
      >
      > now, this works perfectly.
      > but what i would like to do, is disable the button if the value returned
      > is true.
      > Like this:
      > else
      > {
      > document.getEle mentById('lbl') .innerHTML = "Loading... ";
      > document.getEle mentbyId('btn') .enables = false;
      > return true;
      > }
      >
      > But when i try this, the form will not submit.
      > How can i achieve what i'm trying to do here?
      >
      > Submit a form only if validated, and disable the button if so?
      >
      > TIA
      >
      > Grant
      >
      >[/color]


      Comment

      • Edwin Knoppert

        #4
        Re: Basic JavaScript Question

        Postback is desired at that time so why not disable from the server in the
        page_load?



        "Grant Merwitz" <grant@workshar e.com> schreef in bericht
        news:eneMFgYLGH A.2828@TK2MSFTN GP12.phx.gbl...[color=blue]
        > hi
        >
        > I am using Javascript to do my validation
        > This is so i can display a loading message after a form is submitted.
        >
        > Now, currently i do so like this.
        >
        > I have a button, textbox and label like so:
        >
        > <asp:textbox id="tb" runat="server"/>
        > <asp:Button id="btn" runat="server" text="Submit" />
        > <asp:Label id="lbl" runat="server" />
        >
        > I then have a javascript function like so:
        >
        > function validate()
        > {
        > if(document.get ElementById('tb ').value == '')
        > {
        > document.getEle mentById('lbl') .innerHTML = "please fill in
        > textbox";
        > return false;
        > }
        > else
        > {
        > document.getEle mentById('lbl') .innerHTML = "Loading... ";
        > return true;
        > }
        > }
        >
        > And i attach this to the button in the code behind like so:
        >
        > btn.Attributes. Add("onclick", "JavaScript : return validate();");
        >
        >
        > now, this works perfectly.
        > but what i would like to do, is disable the button if the value returned
        > is true.
        > Like this:
        > else
        > {
        > document.getEle mentById('lbl') .innerHTML = "Loading... ";
        > document.getEle mentbyId('btn') .enables = false;
        > return true;
        > }
        >
        > But when i try this, the form will not submit.
        > How can i achieve what i'm trying to do here?
        >
        > Submit a form only if validated, and disable the button if so?
        >
        > TIA
        >
        > Grant
        >
        >[/color]


        Comment

        • Grant Merwitz

          #5
          Re: Basic JavaScript Question

          Thanks this sounds like what i need.

          I've used the form.submit before, but then everything has to be in the
          page_load method.
          This sounds like a smarter option!

          Thanks!

          "slagomite" <slagomite@gmai l.com> wrote in message
          news:1139499259 .018631.141490@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Hrm, interesting. I was able to replicate this -- apparently if the
          > calling object gets disabled, the browser cancels any subsequent
          > actions once control leaves the validate() function.
          >
          > I was able to work around this by explicitly calling the
          > "__doPostBack() " method in the validate() function -- making an
          > explicit call to this should be identical to the form itself being
          > posted (if you create a LinkButton control and look at the page source,
          > you'll see that ASP.NET just does the same thing -- the LinkButton's
          > "onclick" event makes an explicit call to __doPostBack(), with its ID
          > as the argument).
          >
          > So try this (note you'll have to create this script from the
          > server-side -- i.e., in the Page.PreRender handler -- in order to get
          > the reference to the postback method):
          >
          > StringBuilder sb = new StringBuilder() ;
          > sb.Append("<scr ipt language='javas cript'>\r\n");
          > sb.Append("func tion validate()\r\n" );
          > sb.Append("{\r\ n");
          > sb.Append(" if(document.get ElementById('tb ').value == '')\r\n");
          > sb.Append(" {\r\n");
          > sb.Append(" document.getEle mentById('lbl') .innerHTML = 'Try
          > again...';\r\n" );
          > sb.Append(" return false;\r\n");
          > sb.Append(" }\r\n");
          > sb.Append(" else\r\n");
          > sb.Append(" {\r\n");
          > sb.Append(" document.getEle mentById('lbl') .innerHTML =
          > 'Loading...';\r \n");
          > sb.Append(" ");
          > sb.Append(this. GetPostBackEven tReference(btn) ; sb.Append(";\r\ n");
          > sb.Append(" return true;\r\n");
          > sb.Append(" }\r\n");
          > sb.Append("}\r\ n");
          > sb.Append("</script>\r\n");
          > this.RegisterCl ientScriptBlock ("_PageScripts" , sb.ToString());
          > btn.Attributes["onclick"] = "return validate();";
          >
          > HTH,
          > Luke
          >[/color]


          Comment

          • Grant Merwitz

            #6
            Re: Basic JavaScript Question

            Thanks thats great.

            Nice to see you giving back to the community, I strive to do so myself.
            If only i could find the time between work, eat and sleep :)

            This looks like what i need, i have downloaded it and will try integrate it
            into my code

            Thanks Again!

            "S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate. com> wrote in
            message news:uARaz4YLGH A.2904@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Grant,
            >
            > I have a free javascript component on my website (comes with all source
            > code) that has as one of its methods a disable submit script. The code
            > hasn't been updated for .net 2.0 yet, but it's an easy conversion from 1.1
            > to 2.0. I just haven't had time to do it yet. If you take that code and
            > add your javascript for displaying your message to it you'll have what you
            > need. Another nice feature of the script is that before it disables the
            > submit button it first calls the microsoft javascripts that handle page
            > validation so that the button won't be disabled if the page isn't valid.
            >
            > If you have any questions about converting the script or adding your bit
            > of javascript for your load message to it feel free to email me.
            >
            > You may see a demo of the script (#3) and download the entire component as
            > a .net 1.1 project from here:
            > http://www.aboutfortunate.com?page=javascriptdemo
            >
            >
            > --
            > Sincerely,
            >
            > S. Justin Gengo, MCP
            > Web Developer / Programmer
            >
            > www.aboutfortunate.com
            >
            > "Out of chaos comes order."
            > Nietzsche
            > "Grant Merwitz" <grant@workshar e.com> wrote in message
            > news:eneMFgYLGH A.2828@TK2MSFTN GP12.phx.gbl...[color=green]
            >> hi
            >>
            >> I am using Javascript to do my validation
            >> This is so i can display a loading message after a form is submitted.
            >>
            >> Now, currently i do so like this.
            >>
            >> I have a button, textbox and label like so:
            >>
            >> <asp:textbox id="tb" runat="server"/>
            >> <asp:Button id="btn" runat="server" text="Submit" />
            >> <asp:Label id="lbl" runat="server" />
            >>
            >> I then have a javascript function like so:
            >>
            >> function validate()
            >> {
            >> if(document.get ElementById('tb ').value == '')
            >> {
            >> document.getEle mentById('lbl') .innerHTML = "please fill in
            >> textbox";
            >> return false;
            >> }
            >> else
            >> {
            >> document.getEle mentById('lbl') .innerHTML = "Loading... ";
            >> return true;
            >> }
            >> }
            >>
            >> And i attach this to the button in the code behind like so:
            >>
            >> btn.Attributes. Add("onclick", "JavaScript : return validate();");
            >>
            >>
            >> now, this works perfectly.
            >> but what i would like to do, is disable the button if the value returned
            >> is true.
            >> Like this:
            >> else
            >> {
            >> document.getEle mentById('lbl') .innerHTML = "Loading... ";
            >> document.getEle mentbyId('btn') .enables = false;
            >> return true;
            >> }
            >>
            >> But when i try this, the form will not submit.
            >> How can i achieve what i'm trying to do here?
            >>
            >> Submit a form only if validated, and disable the button if so?
            >>
            >> TIA
            >>
            >> Grant
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Grant Merwitz

              #7
              Re: Basic JavaScript Question

              because the process takes a while to run, and that causes users to click the
              submit button again.

              Anyway, after the button is submitted, they are redirected to a success page

              "Edwin Knoppert" <news@hellobasi c.com> wrote in message
              news:43eb64c9$0 $2027$ba620dc5@ text.nova.plane t.nl...[color=blue]
              > Postback is desired at that time so why not disable from the server in the
              > page_load?
              >
              >
              >
              > "Grant Merwitz" <grant@workshar e.com> schreef in bericht
              > news:eneMFgYLGH A.2828@TK2MSFTN GP12.phx.gbl...[color=green]
              >> hi
              >>
              >> I am using Javascript to do my validation
              >> This is so i can display a loading message after a form is submitted.
              >>
              >> Now, currently i do so like this.
              >>
              >> I have a button, textbox and label like so:
              >>
              >> <asp:textbox id="tb" runat="server"/>
              >> <asp:Button id="btn" runat="server" text="Submit" />
              >> <asp:Label id="lbl" runat="server" />
              >>
              >> I then have a javascript function like so:
              >>
              >> function validate()
              >> {
              >> if(document.get ElementById('tb ').value == '')
              >> {
              >> document.getEle mentById('lbl') .innerHTML = "please fill in
              >> textbox";
              >> return false;
              >> }
              >> else
              >> {
              >> document.getEle mentById('lbl') .innerHTML = "Loading... ";
              >> return true;
              >> }
              >> }
              >>
              >> And i attach this to the button in the code behind like so:
              >>
              >> btn.Attributes. Add("onclick", "JavaScript : return validate();");
              >>
              >>
              >> now, this works perfectly.
              >> but what i would like to do, is disable the button if the value returned
              >> is true.
              >> Like this:
              >> else
              >> {
              >> document.getEle mentById('lbl') .innerHTML = "Loading... ";
              >> document.getEle mentbyId('btn') .enables = false;
              >> return true;
              >> }
              >>
              >> But when i try this, the form will not submit.
              >> How can i achieve what i'm trying to do here?
              >>
              >> Submit a form only if validated, and disable the button if so?
              >>
              >> TIA
              >>
              >> Grant
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • S. Justin Gengo [MCP]

                #8
                Re: Basic JavaScript Question

                Grant,

                You're Welcome.

                You get to sleep huh? Lucky dog.

                --
                Sincerely,

                S. Justin Gengo, MCP
                Web Developer / Programmer



                "Out of chaos comes order."
                Nietzsche
                "Grant Merwitz" <grant@workshar e.com> wrote in message
                news:u7HVpDZLGH A.2992@tk2msftn gp13.phx.gbl...[color=blue]
                > Thanks thats great.
                >
                > Nice to see you giving back to the community, I strive to do so myself.
                > If only i could find the time between work, eat and sleep :)
                >
                > This looks like what i need, i have downloaded it and will try integrate
                > it into my code
                >
                > Thanks Again!
                >
                > "S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate. com> wrote
                > in message news:uARaz4YLGH A.2904@TK2MSFTN GP10.phx.gbl...[color=green]
                >> Grant,
                >>
                >> I have a free javascript component on my website (comes with all source
                >> code) that has as one of its methods a disable submit script. The code
                >> hasn't been updated for .net 2.0 yet, but it's an easy conversion from
                >> 1.1 to 2.0. I just haven't had time to do it yet. If you take that code
                >> and add your javascript for displaying your message to it you'll have
                >> what you need. Another nice feature of the script is that before it
                >> disables the submit button it first calls the microsoft javascripts that
                >> handle page validation so that the button won't be disabled if the page
                >> isn't valid.
                >>
                >> If you have any questions about converting the script or adding your bit
                >> of javascript for your load message to it feel free to email me.
                >>
                >> You may see a demo of the script (#3) and download the entire component
                >> as a .net 1.1 project from here:
                >> http://www.aboutfortunate.com?page=javascriptdemo
                >>
                >>
                >> --
                >> Sincerely,
                >>
                >> S. Justin Gengo, MCP
                >> Web Developer / Programmer
                >>
                >> www.aboutfortunate.com
                >>
                >> "Out of chaos comes order."
                >> Nietzsche
                >> "Grant Merwitz" <grant@workshar e.com> wrote in message
                >> news:eneMFgYLGH A.2828@TK2MSFTN GP12.phx.gbl...[color=darkred]
                >>> hi
                >>>
                >>> I am using Javascript to do my validation
                >>> This is so i can display a loading message after a form is submitted.
                >>>
                >>> Now, currently i do so like this.
                >>>
                >>> I have a button, textbox and label like so:
                >>>
                >>> <asp:textbox id="tb" runat="server"/>
                >>> <asp:Button id="btn" runat="server" text="Submit" />
                >>> <asp:Label id="lbl" runat="server" />
                >>>
                >>> I then have a javascript function like so:
                >>>
                >>> function validate()
                >>> {
                >>> if(document.get ElementById('tb ').value == '')
                >>> {
                >>> document.getEle mentById('lbl') .innerHTML = "please fill in
                >>> textbox";
                >>> return false;
                >>> }
                >>> else
                >>> {
                >>> document.getEle mentById('lbl') .innerHTML = "Loading... ";
                >>> return true;
                >>> }
                >>> }
                >>>
                >>> And i attach this to the button in the code behind like so:
                >>>
                >>> btn.Attributes. Add("onclick", "JavaScript : return validate();");
                >>>
                >>>
                >>> now, this works perfectly.
                >>> but what i would like to do, is disable the button if the value returned
                >>> is true.
                >>> Like this:
                >>> else
                >>> {
                >>> document.getEle mentById('lbl') .innerHTML = "Loading... ";
                >>> document.getEle mentbyId('btn') .enables = false;
                >>> return true;
                >>> }
                >>>
                >>> But when i try this, the form will not submit.
                >>> How can i achieve what i'm trying to do here?
                >>>
                >>> Submit a form only if validated, and disable the button if so?
                >>>
                >>> TIA
                >>>
                >>> Grant
                >>>
                >>>[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                • Grant Merwitz

                  #9
                  Re: Basic JavaScript Question

                  Worked perfectly!!!!

                  "Grant Merwitz" <grant@workshar e.com> wrote in message
                  news:ONeUJEZLGH A.3732@TK2MSFTN GP10.phx.gbl...[color=blue]
                  > Thanks this sounds like what i need.
                  >
                  > I've used the form.submit before, but then everything has to be in the
                  > page_load method.
                  > This sounds like a smarter option!
                  >
                  > Thanks!
                  >
                  > "slagomite" <slagomite@gmai l.com> wrote in message
                  > news:1139499259 .018631.141490@ z14g2000cwz.goo glegroups.com.. .[color=green]
                  >> Hrm, interesting. I was able to replicate this -- apparently if the
                  >> calling object gets disabled, the browser cancels any subsequent
                  >> actions once control leaves the validate() function.
                  >>
                  >> I was able to work around this by explicitly calling the
                  >> "__doPostBack() " method in the validate() function -- making an
                  >> explicit call to this should be identical to the form itself being
                  >> posted (if you create a LinkButton control and look at the page source,
                  >> you'll see that ASP.NET just does the same thing -- the LinkButton's
                  >> "onclick" event makes an explicit call to __doPostBack(), with its ID
                  >> as the argument).
                  >>
                  >> So try this (note you'll have to create this script from the
                  >> server-side -- i.e., in the Page.PreRender handler -- in order to get
                  >> the reference to the postback method):
                  >>
                  >> StringBuilder sb = new StringBuilder() ;
                  >> sb.Append("<scr ipt language='javas cript'>\r\n");
                  >> sb.Append("func tion validate()\r\n" );
                  >> sb.Append("{\r\ n");
                  >> sb.Append(" if(document.get ElementById('tb ').value == '')\r\n");
                  >> sb.Append(" {\r\n");
                  >> sb.Append(" document.getEle mentById('lbl') .innerHTML = 'Try
                  >> again...';\r\n" );
                  >> sb.Append(" return false;\r\n");
                  >> sb.Append(" }\r\n");
                  >> sb.Append(" else\r\n");
                  >> sb.Append(" {\r\n");
                  >> sb.Append(" document.getEle mentById('lbl') .innerHTML =
                  >> 'Loading...';\r \n");
                  >> sb.Append(" ");
                  >> sb.Append(this. GetPostBackEven tReference(btn) ; sb.Append(";\r\ n");
                  >> sb.Append(" return true;\r\n");
                  >> sb.Append(" }\r\n");
                  >> sb.Append("}\r\ n");
                  >> sb.Append("</script>\r\n");
                  >> this.RegisterCl ientScriptBlock ("_PageScripts" , sb.ToString());
                  >> btn.Attributes["onclick"] = "return validate();";
                  >>
                  >> HTH,
                  >> Luke
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  Working...