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="é" />
<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 "&". It looks like asp.net transform the "&" (from "é") into the html equivalent ‘&’.
If I set it at design time:
<asp:Button ID="Button1" runat="server" Text="élè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="&#233;l&#232;ves" id="Button1" />
<br />
<span id="Label1">élèves</span>
</div>
I have found this: same question here but the solution does not work
2
Answers
this line of code makes no sense:
either you want this:
Or say this:
or, with encoding, then try this:
The button text does not require encoding. This works in my system: