skip to Main Content

I’m writing an asp.net page with to webcontrols, a label and a button.

 <form id="form1" runat="server">
       <div>
            <asp:Button ID="Button1" runat="server" Text="&eacute;" />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       </div>
   </form>

In the page load event, I’m trying to fill the text property with special caracters:

 protected void Page_Load(object sender, EventArgs e)
   {

           string text = System.Net.WebUtility.HtmlEncode("élèves");
           Label1.Text = Button1.Text = text;
   } 

When I browse the file, I wonder why the label displays "éléves" but the button displays "élèves".
In the browser, the source code of the page, button’s text starts with "&amp;". It looks like asp.net transform the "&" (from "é") into the html equivalent ‘&amp;’.

If I set it at design time:

<asp:Button ID="Button1" runat="server" Text="&eacute;l&egrave;ves" />

The caption of the button is "élèves" like expected

What happens ? … how to add special caracters in the text of a button at runtime ?

I expected to have the right (french) text: "élèves" caption for the button (like the label).

I have been searching on the net for a moment but no result. I have just tested to set the page directive VALIDATEREQUEST to false but no success.

Here is the source code interpreted/received by the browser:

    <div>
       <input type="submit" name="Button1" value="&amp;#233;l&amp;#232;ves" id="Button1" />
       <br />
       <span id="Label1">élèves</span>
    </div>

The result on the screen is:enter image description here

I have found this: same question here but the solution does not work

2

Answers


  1. this line of code makes no sense:

    Label1.Text = Button1.Text = text;
    

    either you want this:

    string mytext = "abc";
    Label1.Text = mytext;
    button1.Text = mytext;
    

    Or say this:

    Label1.Text = "abc";
    button1.Text = Lable1.Text;
    

    or, with encoding, then try this:

     Button1.Text = Server.HtmlDecode("&eacute;l&egrave;ves");
    
    Login or Signup to reply.
  2. The button text does not require encoding. This works in my system:

    Button1.Text = "élèves"
    Label1.Text = System.Net.WebUtility.HtmlEncode("élèves")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search