Trido
Trido

Reputation: 545

Changing button text with Javascript

I have the below code:

<asp:Content ID="HeadContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
        function SetText(id) {
            if (Button2.value == "Disable automatic page refresh")
                Button2.value = "Automatic Refresh Disabled";
            return false;
        }
    </script>
</asp:Content>

<asp:Button ID="Button2" runat="server" Text="Disable automatic page refresh" OnClick="Button2_Click" OnClientClick="return SetText(this)" />

When I click the button though, the button name does not change, but the code behind C# does still work as normal. Can anyone point me in the right direction? I thought it might have been the OnClick event, but after removing it, it still didn't work. I also tried changing the OnClick to OnServerClick just in case but to no avail.

Upvotes: 2

Views: 9623

Answers (2)

feelimann
feelimann

Reputation: 1

   function SetText(id) {
        if (id.value == "text one") {
            id.value = "text two";
        }
        else
            id.value = "text one"; //return to the previous value.
        return false;
    }

Upvotes: 0

Guffa
Guffa

Reputation: 700342

You are using the name of the button instead of the reference to the button that is sent to the method. Use the reference:

function SetText(id) {
  if (id.value == "Disable automatic page refresh") {
    id.value = "Automatic Refresh Disabled";
  }
  return false;
}

Upvotes: 7

Related Questions