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. Set label's text clientside and use it to set button's text server side.

Set label's text clientside and use it to set button's text server side.

Scheduled Pinned Locked Moved ASP.NET
questionjavascripthtmlsysadmintools
7 Posts 2 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 Button1, Button2 gets enabled and the label's text is set to "bye". This is done with a bit of javascript. When Button2 is clicked, it's text should be set to "bye" - the new text value of the label, but is set to "hello" instead - the original text value of the label. How can I fix this? Aspx:

    <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>
    <input id="Button1" type="button" value="Button1" onclick="Hello()" />
    <br />
    <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2"
    onclick="Button2_Click" />
    <br />
    <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
    </div>
    </form>
    </body>
    </html>

    Cs:

    protected void Button2\_Click(object sender, EventArgs e)
    {
        this.Button2.Text = this.Label1.Text;
    }
    
    J 1 Reply Last reply
    0
    • H Hardus Lombaard

      When I click Button1, Button2 gets enabled and the label's text is set to "bye". This is done with a bit of javascript. When Button2 is clicked, it's text should be set to "bye" - the new text value of the label, but is set to "hello" instead - the original text value of the label. How can I fix this? Aspx:

      <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>
      <input id="Button1" type="button" value="Button1" onclick="Hello()" />
      <br />
      <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2"
      onclick="Button2_Click" />
      <br />
      <asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
      </div>
      </form>
      </body>
      </html>

      Cs:

      protected void Button2\_Click(object sender, EventArgs e)
      {
          this.Button2.Text = this.Label1.Text;
      }
      
      J Offline
      J Offline
      JamieRushton_
      wrote on last edited by
      #2

      Markup - HTML

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <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>
      <input id="Button1" type="button" value="Button1" onclick="Hello()" /><br />

          <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /><br />
          
          <asp:Label runat="server" ID="Label1" Text="hello" />
      </div>
      </form>
      

      </body>
      </html>

      Code-behind

      void Page_Init(object sender, EventArgs e)
      {
      Button2.Click += new EventHandler(Button2_Click);
      }

      protected void Page_Load(object sender, EventArgs e)
      {

      }

      void Button2_Click(object sender, EventArgs e)
      {
      Button button = (Button)sender;
      button.Text = "Something new";
      }

      H 1 Reply Last reply
      0
      • J JamieRushton_

        Markup - HTML

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <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>
        <input id="Button1" type="button" value="Button1" onclick="Hello()" /><br />

            <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /><br />
            
            <asp:Label runat="server" ID="Label1" Text="hello" />
        </div>
        </form>
        

        </body>
        </html>

        Code-behind

        void Page_Init(object sender, EventArgs e)
        {
        Button2.Click += new EventHandler(Button2_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        void Button2_Click(object sender, EventArgs e)
        {
        Button button = (Button)sender;
        button.Text = "Something new";
        }

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

        I need to use the label's new text value in the code behind for further processing. Using 'this.label1.text' doesn't work as it gives the original text value not the new one.

        J 1 Reply Last reply
        0
        • H Hardus Lombaard

          I need to use the label's new text value in the code behind for further processing. Using 'this.label1.text' doesn't work as it gives the original text value not the new one.

          J Offline
          J Offline
          JamieRushton_
          wrote on last edited by
          #4

          Oops sorry. Can you not simply use the code-behind to solve this issue? Because your clicking on a server control the page is reloaded back to its orginal state. If you want to change the text of the button after changing the label control using JavaScript then you will need to change the second button using JavaScript also.

          H 1 Reply Last reply
          0
          • J JamieRushton_

            Oops sorry. Can you not simply use the code-behind to solve this issue? Because your clicking on a server control the page is reloaded back to its orginal state. If you want to change the text of the button after changing the label control using JavaScript then you will need to change the second button using JavaScript also.

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

            No. I am working on an application that utilizes a scheduling control. This control has a client-side click event that I must use. When I click on the control it must set a property wich in turn must be used when a button on the page is clicked. My solution to this was to set a label's text property but that doesn't seem to work, as you have seen in my code. Maybe you can help me out. Thanks anyways.

            J 1 Reply Last reply
            0
            • H Hardus Lombaard

              No. I am working on an application that utilizes a scheduling control. This control has a client-side click event that I must use. When I click on the control it must set a property wich in turn must be used when a button on the page is clicked. My solution to this was to set a label's text property but that doesn't seem to work, as you have seen in my code. Maybe you can help me out. Thanks anyways.

              J Offline
              J Offline
              JamieRushton_
              wrote on last edited by
              #6

              Only other way I can think of is to use a hidden field.

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head runat="server">
              <title>Test</title>
              <script type="text/javascript">
              function Hello() {
              var value = "bye";

                      document.getElementById("<%=Button2.ClientID %>").disabled = false;
              
                      document.getElementById("<%=Label1.ClientID %>").innerText = value;
                      document.getElementById("<%=hdnValue.ClientID %>").value = value;
                  }
              </script>
              

              </head>
              <body>
              <form id="form1" runat="server">
              <div>
              <input id="Button1" type="button" value="Button1" onclick="Hello()" /><br />

                  <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" />
                  
                  <br />
                  
                  <asp:Label runat="server" ID="Label1" Text="hello" />
              
                  <asp:HiddenField ID="hdnValue" runat="server" />
              </div>
              </form>
              

              </body>
              </html>

              void Page_Init(object sender, EventArgs e)
              {
              Button2.Click += new EventHandler(Button2_Click);
              }

              protected void Page_Load(object sender, EventArgs e)
              {
              }

              void Button2_Click(object sender, EventArgs e)
              {
              Button button = (Button)sender;
              button.Text = hdnValue.Value;
              }

              H 1 Reply Last reply
              0
              • J JamieRushton_

                Only other way I can think of is to use a hidden field.

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head runat="server">
                <title>Test</title>
                <script type="text/javascript">
                function Hello() {
                var value = "bye";

                        document.getElementById("<%=Button2.ClientID %>").disabled = false;
                
                        document.getElementById("<%=Label1.ClientID %>").innerText = value;
                        document.getElementById("<%=hdnValue.ClientID %>").value = value;
                    }
                </script>
                

                </head>
                <body>
                <form id="form1" runat="server">
                <div>
                <input id="Button1" type="button" value="Button1" onclick="Hello()" /><br />

                    <asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" />
                    
                    <br />
                    
                    <asp:Label runat="server" ID="Label1" Text="hello" />
                
                    <asp:HiddenField ID="hdnValue" runat="server" />
                </div>
                </form>
                

                </body>
                </html>

                void Page_Init(object sender, EventArgs e)
                {
                Button2.Click += new EventHandler(Button2_Click);
                }

                protected void Page_Load(object sender, EventArgs e)
                {
                }

                void Button2_Click(object sender, EventArgs e)
                {
                Button button = (Button)sender;
                button.Text = hdnValue.Value;
                }

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

                Thank you very much! It works!

                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