troubleshooting Question

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

Avatar of kellyclu
kellyclu asked on
2 Comments1 Solution1002 ViewsLast Modified:
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
            return base.Text;
      //  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);


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros