You should use the button’s onClientClick event handler to change button text:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function changeButtonText(button) {
button.value = "Processing";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" OnClientClick="changeButtonText(this);" />
</form>
</body>
</html>
Counting Characters Realtime in ASP.NET TextBox Using JavaScript
ASPX file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function countCharacters() {
var count = parseInt(document.getElementById('<%=TextBox1.ClientID%>').value.length);
document.getElementById('<%=Label1.ClientID%>').innerHTML = 100 - count;
}
</script>
<style type="text/css">
.style1
{
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Max 100 characters<br />
<span class="style1">Characters left:</span>
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
</body>
</html>
ASPX.CS file
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onKeyUp", "countCharacters()");
}
Counting textbox length with JavaScript
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function countCharacters() {
var count = document.getElementById('<%=TextBox1.ClientID%>').value.length;
alert(count);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return countCharacters();"/>
</form>
</body>
</html>
Related post:
Hide/Show a Button using JavaScript
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function hideButton() {
document.getElementById('<%=Button1.ClientID%>').style.visibility = "hidden";
return false;
}
function showButton() {
document.getElementById('<%=Button1.ClientID%>').style.visibility = "visible";
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return hideButton();"/>
<asp:Button ID="Button3" runat="server" Text="Button" OnClientClick="return showButton();"/>
</form>
</body>
</html>
Change image of imagebutton onMouseOver
ASPX file
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/1.png" />
ASPX.CS file
ImageButton1.Attributes.Add("onmouseover", "this.src='2.png'");
ImageButton1.Attributes.Add("onmouseout", "this.src='1.png'");