ASP.net insert html encoded code into page - asp.net

I have a dim which contains html encoded code, which is stored in DB.
How can I insert the code into asp.net page?
I would like to perform the action in code behind ( VB.net / C# ).
What control/method should I use. Please note I want the code to be rendered by the browsers – not just displayed.

Assuming web forms, you can use a Literal control on your web page and set its Mode property to PassThrough.
PassThrough meaning
The contents of the control are not modified.
as opposed to the default Transform
The contents of the control are converted to an HTML-encoded string.
Example aspx:
<asp:Literal ID="Literal1" Mode="PassThrough" runat="server"></asp:Literal>
Code-behind:
Me.Literal1.Text = "<p>I'm a paragraph.</p>"

Related

Can client-side generated controls affect ViewState?

I have a page that generates new text inputs, checkboxes, selects with JavaScript. So none of these controls has runat="server" set.
I would like to know if these controls are sent to the server on PostBack and become part of the Viewstate, altering it in any way.
The short answer is yes, by assignment.
Dynamically generated plain HTML elements in ASPX page will be treated as LiteralControl elements rather than WebControl elements, hence they don't affect ViewState directly like some ASP .NET server controls. However, since they're placed inside form tag (usually with runat="server" attribute), their values are posted together as postback event stage triggered by submitting the form, which identified as key-value pair in Request.Form collection (the key is recognized by name attribute of literal HTML element).
Suppose you have dynamically generated text box with JS like this:
<input name="FirstName" type="text" />
Then you can retrieve its value using Request.Form:
If Not String.IsNullOrEmpty(Request.Form("FirstName")) Then
Dim firstName As String = Request.Form("FirstName")
And, the important point, you can omit the string assignment above and assign literal text box value to ViewState:
ViewState("FirstName") = Request.Form("FirstName").ToString()
Note that only HTML server controls (e.g. <input runat="server" />) and ASP .NET server controls have control name directly accessible in code-behind and ViewState maintained automatically (unless having EnableViewState attribute set to false).
Additional ViewState reference:
Understanding ASP.NET View State
Related issues:
How to viewstate in normal HTML input in asp.net
Which controls have ViewState maintained?
viewstate for HTML control

“The Controls collection cannot be modified because the control contains code blocks”

While trying to insert controls in my page with the following block of code, i got this exception
The Controls collection cannot be modified because the control
contains code blocks
Literal lite=new Literal();
lite.Text = #"<div class='cookieLaw_slidesharey'><a id='cookieLaw_slideshare' href='#'><img src='/img/content/cookielaw_slideshare.jpg'/></a></div>";
Control control = this.FindControl("PlaceHolder");
control.Controls.AddAt(0,lite);
I tried to follow the advices written in this thread:
"The Controls collection cannot be modified because the control contains code blocks"
Which are basically:
replacing <%= %> with <%# %>
And adding
Page.Header.Databind() in the page load
But i get the following exception:
Object reference not set to an instance of an object.
Thank you for your help.
PS: i'm using .NET 3.5
Check if your "PlaceHolder" has a runat=server attribute. That should already help. You can address that control by it's id then instead of using FindControl.
The reason why your Page.Header.Databind() is not working is probably because you don't have a head section with the runat=server like in the example you copied from. They were adding controls in the header there which can be slightly different.

Asp.net server controls are not changing its text while selecting new culture language

I want to implement localisation in web forms. For this I have created resource files under App_GlobalResourses.
At my web forms both html and asp.net server controls exist. I have replaced both text as shown below respectively:
<%= Resources.Resource.Email%> (for html controls)
and
asp:Button ID="Button2" runat="server" Text="<%$ Resources:Resource, Email %>" /> (for asp.net controls)
Now I have a dropdown for switching culture. When I change culture, all html controls text changed according to new culture but asp.net server controls text is not changing.
How can I change asp.net server controls text.
I had the same problem.
what I did:
(I believe there are better solutions but I did not find them).
I have an asp:button named btnSubmit. its text comes from "Resources.MyResource.SubmitText".
in document.ready() function I add the follwing line:
$("#<%= btnSubmit.ClientId %>").val("<%= Resources.MyResource.SubmitText %>");
so when the page is ready the value of my button is changed to the current resource,
and it works perfectly.

How do I encode HTML in an ASP.NET repeater?

I have an ASP.NET repeater pulling comment data from a database.
In my ItemTemplate I placed some Label server controls bound to the fields (username of poster, date, and post text), but apparently Label does not run the data through HtmlEncode before displaying it.
Is there another control I should use? How should I display HTML-encoded data from a repeater?
What about literal with mode="encode"
<asp:Literal ID="Literal1" runat="server" Mode="Encode" />
You can use the literal control which has a mode property with enumeration Encode,PassThrough,Transform.
This has worked for me:
<%# Server.HtmlDecode(DataBinder.Eval(Container.DataItem, "ItemName")) %>
I'm assuming you want to be able to display the comments in html (with formatting et al).
replace the Label control with an Literal control. They both have the Text property but the control will handle your html content.
<asp:Literal>

Proper syntax for asp.net embedded Resource tags

I'm trying to localize an asp.net web application. Consider the following asp.net code. I'm running with CurrentCulture and CurrentUICulture set to German ("DE-DE").
<%= ReportTitles.EndOfDay %>
<asp:Literal ID="litLabel" runat="server" Text="<%$ Resources:ReportTitles, EndOfDay %>"/>
I expect these two lines to yield the same result, but instead I get this:
Auswertungen für den Tagesabschluss
End of Day
In other words, the first syntax <%= ReportTitles.EndOfDay %>successfully retrieves the value from the ReportTitles.de.resx file we're using, but the second syntax <asp:Literal ID="litLabel" runat="server" Text="<%$ Resources:ReportTitles, EndOfDay %>"/>
gets the value out of the default US English ReportTitles.resx file.
What's wrong with the 2nd line? Thanks
Setting culture during Load is somewhere in the middle of the life cycle. This is after the literal control has been created.
Basically, the control tree is created when Page.ProcessRequest calls FrameworkInitialize, which calls __BuildControlTree, a method autogenerated from your code file. It will instantiate a new Literal control, set all properties, and add it to the control tree. It has basically already read from the active ResourceManager. This is before Load, it's even before PreInit. This means that you havn't changed culture yet.
<%= ... %> will be parsed into a call to HtmlTextWriter.Write during render. This is at the end of the life cycle, and it will use the new culture.
The usual place to implement this is either using a HttpModule/HttpHandler or overriding Page.InitializeCulture.
Check out http://ghferr.free.fr/wiki/Articles/images/aspnet_page-control-life-cycle.jpg, a overview worth printing and putting on a wall in your vicinity. ;)

Resources