Link to home
Start Free TrialLog in
Avatar of kellyclu
kellyclu

asked on

how can i override textbox length to recognize foreign chars as 2 in length? (c# 2005)

trying to create a textbox that will count foreign chars as 2 bytes, then use tagmap in config to replace all textboxes in app.  if textbox max length is set to 4, and you enter 3 foreign chars, .net considers 3 characters entered.  i'm trying to override this behavior to say foreign chars are 2, ascii chars are one.

So if you enter 2 foreign chars, and 2 english chars, text.length is 6.  problem is that length is readonly property, so i'm trying by setting text, but having trouble.  

here's what i have so far:

public class TextBox : System.Web.UI.WebControls.TextBox
{
    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
       
      //  base.Text.Length = ASCIIEncoding.UTF8.GetBytes(value.ToCharArray()).Length;  // compiler err that length is readonly

          //  base.Text = value;
        }
    }

    protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
    {
        Page.ClientScript.ValidateEvent(this.UniqueID, string.Empty);

        string _presentValue = this.Text;
        string _postedValue = postCollection[postDataKey];

        if (_postedValue.Length > this.MaxLength)
        {
            _postedValue = _postedValue.Substring(0, this.MaxLength);
        }

        if (!_presentValue.Equals(_postedValue))
        {
            this.Text = _postedValue;
            return true;
        }

        return false;
        //return base.LoadPostData(postDataKey, postCollection);
    }
}


ASKER CERTIFIED SOLUTION
Avatar of ChetOS82
ChetOS82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kellyclu
kellyclu

ASKER

there has to be a way.  javascript isn't the answer for this case because i have over 40 pages in project, some with dozens of textboxes.

i think this might do it, still setting up test.

    public class TextBox : System.Web.UI.WebControls.TextBox
    {
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = ASCIIEncoding.UTF8.GetString(ASCIIEncoding.UTF8.GetBytes(value.ToCharArray()));
            }
        }