Thursday, July 14, 2011

Coundown text (Multiline) left from Textbox in asp.net

Source page:
=========
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="textboxcountdown.aspx.cs" Inherits="textboxcountdown" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">
    function textCounter(field, countfield, maxlimit) {
        var MaxLength = 100;
        var Label1 = document.getElementById(countfield);
        if (field.value.length > maxlimit)
            field.value = field.value.substring(0, maxlimit);
        Label1.innerText = (maxlimit - field.value.length) + ' Characters Remaining';
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" Height="128px" MaxLength="1000"
            TextMode="MultiLine" Width="313px" ></asp:TextBox><br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
Code behindWindow:
===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class textboxcountdown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     TextBox1.Attributes.Add("onkeydown", "textCounter(this,'" + Label1.ClientID + "', 100)");
    TextBox1.Attributes.Add("onkeyup", "textCounter(this,'" + Label1.ClientID + "', 100)");
    TextBox1.Attributes.Add("onmousedown", "textCounter(this,'" + Label1.ClientID + "', 100)");
    TextBox1.Attributes.Add("onmouseup", "textCounter(this,'" + Label1.ClientID + "', 100)");
    TextBox1.Attributes.Add("onblur", "textCounter(this,'" + Label1.ClientID + "', 100)");
    }

}

No comments:

Post a Comment