Wenadin
Wenadin

Reputation: 396

Text attribute of an asp label using JavaScript is not staying changed

I have a button:

<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick()"/>

that runs the following JavaScript function when it gets clicked:

function ResetClick() {
    document.getElementById("<%=lblError.ClientId%>").innerHTML = "";
}

That changes the text on this label:

<asp:Label ID="lblError" runat="server" Style="color: Red;" Text=""></asp:Label>

Unfortunately, my code behind also changes the label text to an error message using the following code:

lblError.Text = "No data is available"

When I click the reset button, the text is momentarily cleared and then it fills again with the error message. Is there any way to have the code behind set a message in the label and let the JavaScript clear the message?

Upvotes: 1

Views: 388

Answers (1)

Popo
Popo

Reputation: 2460

If you do not want the button to post back then you should add return false; to the OnClientClick attribute:

<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick(); return false;"/>

PS: adding as an answer since it sounds like it was helpful.

Upvotes: 1

Related Questions