ASP.NET: How to avoid parsing part of a page - asp.net

I have a section in an ASP.NET page that has some <% and %> that I do NOT want the ASP.NET compiler to process. How can I specify a section to not be parsed?

You can comment those sections like so:
<%-- (anything to be ignored) --%>
Or if you mean you literally have the <% and %> characters in your page, you should always use < and > instead of < and >.

There's not a general built-in way to accomplish this. However, if there is no complex logic in the code blocks, you can try the following:
overload rendering of the page (for example load it in a master page)
open the .ASPX file and manually read in the text and put it in a StringBuilder
identify and HTMLEncode the code blocks before inserting into the StringBuilder
feed the resulting text into Server.Execute() as if dynamically rendering a page or custom control.

Not quite sure what you mean, but you can escape the < and > using their HTML entity equivalents - < and >.

Related

displaying session value instead of code itself in aspx

<%= Session.Item("user_fullname")%>
this supposed to display session value right?
but mine is displaying code itself....
Note: I have included aspx page in another aspx, like:
<% Response.WriteFile("../etc/header1.aspx") %>
the displaing session code is inside header1.aspx
anyone knows how to display session value?
Session items would be stored as an Object. You need to cast to the correct type:
<%= (string)Session.Item("user_fullname") %>
When using Response.WriteFile("../etc/header1.aspx"), the usual Page Life cycle will NOT be executed for the mentioned header1.aspx page.
Since there will be no asp.net page life cycle processing , the inline code
<% %> won't be executed at all and therefore all such statements will be displayed as it is.
Response.WriteFile() is used to write just the Contents of a file, which can contain HTML , Controls, directly to the output stream.

How to pass an object from .cs to .aspx

I am a asp .net beginner. I want to use some objects created at Site.Master.cs in Site.Master. Is there an easy way to do it?
I know how to do it in MVC(by using view(the object)). But how can i do it in normal ASP .net web application?
I don't understand what exactly you want to do.
If you want to insert some string into tag's title you can insert the following thing in SiteMaster.master file:
<img src="<%= Page.ResolveUrl("~/") %>images/logo.png">
instead of:
<img src="images/logo.png">
In the first case there will be calculated the path from the root of your application. In the second case there will be relative link. This is because server will CALCULATE the value of Page.ResolveUrl("~") function and will WRITE it in src tag.
You can do the same thing with any other methods, classes if you defined them properly. But I wouldn't recommend you to implement complicated logic in .aspx files (or .master files). Because you can end up with many difficulties with testing and styling such application.
There are other server tags:
<% %> - an embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. Description and examples
<%= %> - most useful for displaying single pieces of information. Description and examples
<%# %> - data binding expression syntax. Description and examples
<%$ %> - ASP.NET Expression. Description and examples
<%# %> - Directive Syntax. Description and examples
<%-- --%> - Server-Side Comments. Description and examples
<%: %> like <%= %> - But HtmlEncodes the output (new with Asp.Net 4). Description and examples
Another way: you can use JSON to send some data to the client and then process it with javascript. Take a look at this project.
If the #Page directive in your .aspx file has Inherits="XYZ" where XYZ is the class declared in your .cs file, you can simply add a protected field to your class and assign a value to it. You'll be able to access it in the .aspx file just by using its name.
You can also use HttpContext.Items property to keep objects during a single request:
HttpContext.Current.Items["SavedItem"] = "hello world";
And use it in page:
<%= ((string)Context.Items["SavedItem"]) %>
Any public or protected property or method in Site.Master.cs will be accessible from Site.Master.
but how to invoke c# code in aspx ?
There are several ways, including the <%= %> construction, and databinding syntax.
It would help if you explained what you're trying to achieve.

Displaying html page in ASPX page

I need to display dynamic created html in aspx(server side html);
I tried to use Iframe but it will not displaying anything ; it will not work because of security reasons ;
Is there is any controls that will display html page? Dynamic html have its on css and javascripts so I can’t use html text box controls.
If anyone have solution please help
Thanks
Since your dynamic page has its own CSS & Javascript, I'm assuming it's not written to coexist with its host page. I'm also assuming that when you tried to use the iFrame you just tried to write straight to it from the containing page.
I would suggest moving your code that generates the HTML to a separate ASPX page and referencing that page as the source of your iFrame or rewriting your CSS & Javascript so it will coexist and using a DIV.
Also, it's sort of hard to come up with a workable solution without you showing some of the code you have currently.
Have a look at the
<asp:Literal>
control. There's an example here: Set ASP Literal text with Javascript
-- EDITED 03/05/2012 --
Simple example of an asp.net literal control in action:
.aspx code
<asp:Literal ID="MyLiteral" runat="server" />
.vb code behind
Dim k As String
k = "<table style=""border: 1px solid red;""><tr><td>Cell 1</td></tr><tr><td>Cell 2</td></tr></table>"
MyLiteral.Text = k
If I compile this in VS2008 I get a two row table with a red border in IE.
I found the answer!! Use UFRAME!! It's simple and easy!! uframe.codeplex.com

How do I put a placeholder twice on master page?

I have a master page and I want to add a placeholder twice - so that I have the same placeholder in two places of the master page so that actual page just specifies content of the placeholder once and that content is rendered twice on the resulting page. The goal is to avoid duplication of content.
If I try to add a placeholder with the same id twice it won't compile - ASP.NET doesn't like that.
How do I achieve that? What are other options?
Place your placeholder in an Action, and call it where necessary.
<%
Action myPlaceholder = () =>
{%>
<asp:ContentPlaceHolder ID="X" runat="server" />
<%}
%>
...then call wherever necessary in code.
<% if (conditionMet)
myPlaceholder(); >%
The error occurs at compile time: a complaint will be made if placeholders with the same ID exist. This approach clears that hurdle, and gives a lot of flexibility.
I think you are probably looking for a user control:
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
With User Controls you can add your markup and code behind into an .ascx and then call this in your Master Page/Web Form multiple times, therefore avoiding duplicate code.
I haven't tried this, but you could lookup the contents of the placeholder you are interested in duplicating, and copying it in your code behind. This post: http://programcsharp.com/blog/archive/2009/01/22/test-if-masterpage-contentplaceholder-has-content-or-is-empty.aspx
has some good insight into manipulating master pages.

Simple HTML construction in ASP.NET?

A simple question, I think:
I want to put a tag into an ASP.NET app I've been asked to maintain, so I'm coming at this from a newbie point of view just tinkering around the edges without knowing a lot.
I wrote an old ASP application back in 1998, so I am just running on memory...
How do I write some output to the webpage?
I know I can use an
<asp:label id="blah">
but then I need to define a Label blah; in my code behind and then assign it.
I believe that I can put in-place:
<% Response.Write("sometext"); %>
and that will write sometext in the location within the page. (Am I correct?)
Lastly, I remember there was a syntax to the effect of
<%= "some string" %>
but I can't find the documentation on it, to say either it is deprecated, unadvised, or the rationale for such a decision.
I have tried googling for "ASP.NET grammar" but I can't even find a good description that "<%=" even exists, though it is mentioned in a few blogs.
For something simple, like inject the global version number, or the current date, then I can't see anything particularly wrong with in-place composition - it would save me defining 15 labels and having to initialise them all - though perhaps the asp:label approach could reference one global instance of a label?
Just asking for opinions on good practices :)
<%= string %> is perfectly valid ASP.NET syntax. The reason you will often find references to problems with using that is people use <%= (equivalent to Response.Write) when they should use <%# (for databinding) or vice-versa.
For example, we use it very extensively in our content managed site, where we pull in values from a global settings repository:
<%= SiteContext.Current.GetSetting("SiteTitle") %>
MSDN:
MSDN entry on <%= (this is under the JScript.NET section but still applies)
MSDN entry on <%#
Some others suggest <%= is not a "best practice" or a very good approach, but I strongly disagree with that sentiment. For an MVC-ish type site (especially a site that is template- or view-driven in some way), the most direct approach is frequently more effective than using server controls.
Just be mindful that when you use an <asp:Label /> it renders the .Text inside the <span> tag whereas an <asp:Literal /> adds no extraneous HTML to the string passed to it.
For example, if you were building a content management system and wanted to display user-driven HTML, a Label control would not correctly display the output from a WYSIWYG type rich textbox whereas a Literal control is the appropriate choice.
The <%= %> is the late-bound equivalent of the Literal's .Text property. The only difference here is when the value is placed in the page (aside from obvious syntax and separation of concerns paradigm) during the course of the page lifecycle.
Since the .Text property is on a control inherited from WebControl, it can be set/read/manipulated during any of the events following the control's Load event (wherever/whenever you load the control inside the page), but the <%= %> text cannot be directly read/used/manipulated by the code-behind without referencing some other control to get to it (like a containing div's InnerHtml property).
There are lots of options. You could use a single label, and string concatenate all the data you want displayed in that location.
You could create a user control with the layout you want and assign values that way.
You could inject it directly with response.write or the <%= %> syntax
You could create an HtmlGenericControl in your code behind (it's a div), add some text to it, and inject it into the pages controls collection.
Whatever you pick, try and go with the existing style of the coded page.
Look up the term "render blocks" for the <% %> syntax.
How about using
<asp:Literal id="z" text="goofy" runat="server" />?
Labels are usually used with forms.
You can also take full control of the rendering of your pages and controls and compose whatever you need to. You control the HTML, the order of rendering your controls, etc...
Go with the <asp:label /> (or a literal control if you want to customize some html in the content). Seriously. I'ts not that hard: when you put label in your markup visual studio will create it in the code-behind for you, so there's no extra work involved.
You could use the <%= "some string" %> syntax in web forms, but there can be issues when mixing that with the asp controls and there's a good reason new frameworks moved away from mixing logic like that in with your markup.

Resources