網頁部份:
<head runat="server">
<title>處理ASP.Net 2.0 的TextBox在MultiLine時MaxLength失效問題</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<link href="/css/index.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
//檢查<TEXTAREA>的maxlength
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
C# 的 code 部份:
protected void Page_Load(object sender, EventArgs e)
{
//因為MultiLine的TextBox的屬性MaxLength設定沒作用
//所寫一段JavaScript, 處理MultiLine的TextBox裡的字數限制(200字)
TextBox1.Attributes.Add("maxlength","200");
TextBox1.Attributes.Add("onkeyup","return ismaxlength(this)");
if (!(IsPostBack))
{
}
}
參考自程式設計俱樂部:
http://www.programmer-club.com/pc2020v5/forum/showsametitleN.asp?board_pc2020=aspdotnet&id=16172
2007年8月31日 星期五
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言