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);
    }
}


ASP.NET

Avatar of undefined
Last Comment
kellyclu

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ChetOS82

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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()));
            }
        }
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy