The issue is, when we try to set value to the label from javascript, at first it will accept the new value but on post back, it will be changed to its previous value.
But onkeypress event, it will work untill there is a post back.
When we call a return false to avoid postback on button click the value will stay untill the next postback.
Please try with the following code (It is working, but resets on postback):
Use the following JavaScript functions:
function ChangeText(retval) {
document.getElementById("testLabel").innerText = "New Text (onclick) : Return Value - " + retval;
return retval;
}
function keyPress() {
document.getElementById("testLabel").innerText = "New Text (onkeypress)";
}
Add the following controls to test the function:
<asp:Label ID="testLabel" runat="server" Text="ASP LABEL CONTROL"></asp:Label>
<input type="button" value="HTML btn retval true" onclick="ChangeText(true);" />
<input type="button" value="HTML btn retval false" onclick="ChangeText(false);" />
<asp:Button ID="testButton" runat="server" Text="ASP btn retval false" OnClientClick="javascript:return ChangeText(false);" />
<asp:Button ID="testButton2" runat="server" Text="ASP btn retval true" OnClientClick="javascript:return ChangeText(true);" />
<asp:TextBox ID="testTextBox" runat="server" onkeypress="javascript:keyPress();"></asp:TextBox>
Please feel free to revert back after testing and add your comments on it. :)
:thumbsup: