Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Enable button and change label's text clientside

Enable button and change label's text clientside

Scheduled Pinned Locked Moved ASP.NET
javascripthtmlsysadmintoolsquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hardus Lombaard
    wrote on last edited by
    #1

    When I click button 1, button 2 must be enabled and the label's text must change from "hello" to "bye". This must be done clientside through Javascript. How must I change the following code to get this to work? When I click button 1 the label's text changes but immediately reverts back to it's original value.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Test</title>
    <script type="text/javascript">
    function Hello() {
    document.getElementById('Button2').disabled = false;
    document.getElementById('Label1').innerText = "bye";
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Hello()"/>
    <br />
    <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button" />
    <br />
    <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
    </div>
    </form>
    </body>
    </html>

    N 1 Reply Last reply
    0
    • H Hardus Lombaard

      When I click button 1, button 2 must be enabled and the label's text must change from "hello" to "bye". This must be done clientside through Javascript. How must I change the following code to get this to work? When I click button 1 the label's text changes but immediately reverts back to it's original value.

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <title>Test</title>
      <script type="text/javascript">
      function Hello() {
      document.getElementById('Button2').disabled = false;
      document.getElementById('Label1').innerText = "bye";
      }
      </script>
      </head>
      <body>
      <form id="form1" runat="server">
      <div>
      <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Hello()"/>
      <br />
      <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button" />
      <br />
      <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
      </div>
      </form>
      </body>
      </html>

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      When you click the button the client-side script is being executed properly, however, it also causes a post-back which resets the page. If all you need to do execute client-side script with no post-back then change the asp:button controls to simple html elements <input type=button>


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        When you click the button the client-side script is being executed properly, however, it also causes a post-back which resets the page. If all you need to do execute client-side script with no post-back then change the asp:button controls to simple html elements <input type=button>


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        Ankur m
        wrote on last edited by
        #3

        Or just put a

        return false;

        in the Hello function. That should also work.

        ..Go Green..

        M 1 Reply Last reply
        0
        • A Ankur m

          Or just put a

          return false;

          in the Hello function. That should also work.

          ..Go Green..

          M Offline
          M Offline
          michaelschmitt
          wrote on last edited by
          #4

          Only if he includes the "return.." part in the OnClientClick also. Like:

          <html xmlns="http://www.w3.org/1999/xhtml">
          <head runat="server">
          <title>Test</title>

          <script type="text/javascript">
              function Hello() {
                  document.getElementById('Button2').disabled = false;
                  document.getElementById('Label1').innerText = "bye";
                  return false;       
              }    </script>
          

          </head>
          <body>
          <form id="form1" runat="server">
          <div>
          <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Hello()" />
          <br />
          <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button" />
          <br />
          <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
          </div>
          </form>
          </body>
          </html>

          H A 2 Replies Last reply
          0
          • M michaelschmitt

            Only if he includes the "return.." part in the OnClientClick also. Like:

            <html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
            <title>Test</title>

            <script type="text/javascript">
                function Hello() {
                    document.getElementById('Button2').disabled = false;
                    document.getElementById('Label1').innerText = "bye";
                    return false;       
                }    </script>
            

            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Hello()" />
            <br />
            <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button" />
            <br />
            <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
            </div>
            </form>
            </body>
            </html>

            H Offline
            H Offline
            Hardus Lombaard
            wrote on last edited by
            #5

            Could you perhaps explain the purpose of these return statements?

            T R 2 Replies Last reply
            0
            • H Hardus Lombaard

              Could you perhaps explain the purpose of these return statements?

              T Offline
              T Offline
              T M Gray
              wrote on last edited by
              #6

              Returning a value in a javascript click event handler tells the browser whether or not to continue normal processing of that click. For a submit button, returning false tells it to stop processing a not to actually submit the form. Returning true allows it to continue submitting the form normally. For a hyperlink returning true would allow the browser to navigate to the URL and returning false would leave the user on the curernt page.

              1 Reply Last reply
              0
              • M michaelschmitt

                Only if he includes the "return.." part in the OnClientClick also. Like:

                <html xmlns="http://www.w3.org/1999/xhtml">
                <head runat="server">
                <title>Test</title>

                <script type="text/javascript">
                    function Hello() {
                        document.getElementById('Button2').disabled = false;
                        document.getElementById('Label1').innerText = "bye";
                        return false;       
                    }    </script>
                

                </head>
                <body>
                <form id="form1" runat="server">
                <div>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Hello()" />
                <br />
                <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button" />
                <br />
                <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
                </div>
                </form>
                </body>
                </html>

                A Offline
                A Offline
                Ankur m
                wrote on last edited by
                #7

                Yes. And that's what I said. His Hello() function is called on OnClientClick. I missed the return in OnClientClick part. I am sorry I didn't explain it.

                ..Go Green..

                1 Reply Last reply
                0
                • H Hardus Lombaard

                  Could you perhaps explain the purpose of these return statements?

                  R Offline
                  R Offline
                  raju melveetilpurayil
                  wrote on last edited by
                  #8

                  return statement is to avoid PostBack. "When I click button 1 the label's text changes but immediately reverts back to it's original value." this is because of PostBack

                  Raju.M

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups