“<” in a text box in ASP.NET --> how to allow it?

前端 未结 7 2075
粉色の甜心
粉色の甜心 2020-12-01 18:24

I have a textfield which displays a string which contains < and >. The code throws an error because of that. How can I allow the usage of those chars in my textfield?

相关标签:
7条回答
  • 2020-12-01 19:25

    your problem is,you cannot use html tags in .net controls. so set the ValidateRequest="false" in your aspx page and encode the text before you saving the text.

        //encode
        private string Encode(string text)
        {
            byte[] encodedText = System.Text.Encoding.UTF8.GetBytes(text);
            return System.Convert.ToBase64String(encodedText);
        }
    

    when you retrieving your text make sure to decode the encoded text.

        // Decode:
        private string Decode(string encodedText)
        {
            byte[] decodedText = System.Convert.FromBase64String(encodedText);
            return System.Text.Encoding.UTF8.GetString(decodedText );
        }
    
    0 讨论(0)
提交回复
热议问题