復制代碼 代碼如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"><!--
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
TextBox1.Attributes.Add("onkeyup", "LimitText()");
}
}
// --></script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
<script type="text/javascript"><!--
function LimitText()
{
//得到此時文本框的字符數
var Len=document.getElementById('TextBox1').value.length;
//得到文本框的默認值5
var TxtSize=document.getElementById('TextBox1');
if(TxtSize.size>20)
{
TxtSize.size=20;
TxtSize.value=TxtSize.value.substring(0,20);
document.getElementById('ban').innerText="不能超過20個字符!";
}
else
{
TxtSize.size=Len+1;
}
}
// --></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" size="5"></asp:TextBox>
<span id="ban"></span>
</div>
</form>
</body>
</html>
原文來自:http://blog.csdn.net/ws_hgo