In ASP.NET, What is the 'ASP' code called? - c#

More detail to my question:
HTML and JavaScript are called "client-side code".
C# and VB in the code behind files are termed "server-side code".
So what is inline-asp, and 'runat=server' code blocks called?
<!-- This is called "client-side" -->
<p>Hello World</p>
<script>alert("Hello World");</script>
...
// This is called "server-side"
public void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello World");
}
...
<%-- What is this called ??? --%>
<asp:Label ID="MyLabel" runat="server" />
<% Response.Write("Hello World"); %>
The best term I can come up with is "Web Forms Code".

To be explicit, Microsoft calls them Embedded Code Blocks.
http://msdn.microsoft.com/en-us/library/ms178135.aspx
They are code blocks embeded into the page lifecycle by being called during the Render phase.

The sections of an ASP page that start with <% and end with %> are code render blocks and <script> elements with runat=server are called code declaration blocks. The code inside them is server code.
The parts that begin with <%# are directives. The code render blocks that begin with <%= is just short hand for a call to writer.Write() in the Page.Render() method.

In the ASP section of the MSDN site they are referred to as "script commands", "server side script commands" and "primary script commands".
Below I have included excerpts from the MSDN site and a reference link.
ASP uses the delimiters <% and %> to enclose script commands. Within the delimiters, you can include any command that is valid for the scripting language you are using.
Commands enclosed by delimiters are called primary script commands, which are processed using the primary scripting language. Any command that you use within script delimiters must be valid for the primary scripting language. By default, the primary scripting language is VBScript, but you can also set a different default language.
(http://msdn.microsoft.com/en-us/library/ms524741.aspx)

I call them "server tags" or "server-side tags".
No idea if this is correct or not.

Code in the aspx file is called "the markup". That includes static html, as well. If you want to narrow it down to code within <% %> tags just say "code blocks".
The <% %> tags themselves and similar are called "Bee Stings". Note that this is just for the different types of <% %> tags, not the code blocks you create with them.

Related

ASP.NET dynamically generate HTML with server controls

When i need to set some value to a Javacript, or any other part of the code, i usually use this:
<script>
<%# SomeFunction() %>
</script>
And this also works for HTML in the document body, like...
<somehtmltag property="<%# SomeFunction2() %>">
And in the code behind i create the function that returns a string, with all the necessary code.
If i add some parameter to a user control like:
<ts:PeopleCard ID="us" runat="server" Visible="<%# IsVisivle() %>" />
It also works, but i try to create the entire user control it does not work.
<%# AddUserControl() %>
Function AddUserControl() as String
Return "<ts:PeopleCard ID=""us"" runat=""server"" Visible=""true"" />"
End Function
I understand that this does not work, because this code must be processed by the server to be converted in the actual code.
The final HTML, it shows:
<ts:PeopleCard ID="us" runat="server" Visible="true" />
when it shouldn't, it should show the processed HTML\css by the server.
So my question is, is it possible to create a control this way? Is it possible to force ASP.NET to "re-process" the page, after I changed its contents in code behind?
I understand there's several other ways to do it. Including, creating the user control in conde behind.
But i need to know, if is possible to do this way...
Usually you have a parent tag that is runat server and you can then add your own object to it.
Dim newTag as New PeopleCard
newTag.Visible = true
pnl.Controls.Add(newTag)
An other option I've done is the past is to add a RenderMethod to a control. Each control have a SetRenderMethodDelegate function and it allows you to write directly to the HtmlTextWriter. This won't create an object for the controls you create yourself.

ASPx Inline Expressing <%= %> equates at runtime to %3c%25=

I have a solution with a couple of projects. Both use inline expression syntax, such as:
<p><a runat="server" href="<%=MyProject.Global.PathSite %>">My Link</a></p>
Assuming that MyProject.Global.PathSite equates to
public const String Whatever = #"http://www.myurl.com/";
At both design time and at runtime project A, the first project that I created in the solution, evaluates the expression correctly, while project B, the second project that I created a couple of months later, evaluates the expression as
%3c%25=MyProject.Global.PathSite%20%25%3e
Basically, ASP.Net treads <% %> as HTML text rather than a tag that it should process, while the second one does not.
Any thoughts?
UPDATE:
I reworked the wording of the question to better make sense.
After suffering with this issue for a couple of weeks, I finally figured out the problem.
The problem is that you cannot use <% %> in any tag, where the runat attribute is server. You can use <% %> on client side tags only.
For tags that are runat server, then simply go to the Page_Load event and enter the property through the code behind.
Try putting <%=MyProjectName.ClassName.PublicProperty %> in a literal with runar=server, you could do that with both in fact.

OnClientClick event doesn't register javascript alert from code-behind

Alright, I have an embarrassingly simple question that hopefully has an embarrassingly simple answer. I am trying to get an asp:ImageButton to display an alert message that contains variables from my codebehind. Here's the overly simplified version of the code:
public string AlertMe
{
get
{
return "alert('hi');";
}
}
Now when I try and access it this way:
<asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" OnClientClick='<%=AlertMe%>'/>
I see a postback, but no alert message, as if it can't access my property at all. Meanwhile, these 2 lines:
<%=AlertMe%><br />
<a onclick="<%=AlertMe%>">click this</a>
both work fine and dandy. (The first displays the code for the javascript alert, and the second fires the alert with no issues.)
So my big question is: why does the OnClientClick event for an asp control fail to register the alert? Is there an obvious flaw I'm missing, or do I really need to register the entire event from code-behind?
You can't use the <%= ... %> syntax to set attributes of a Server Control. To set dynamic attributes on Server Controls, you have to set it in code.
this.btn.OnClientClick = this.AlertMe;
This is because <%= ... %> is a shortcut for Response.Write(). So, it isn't called until after the aspx has been parsed, evaluated, and rendering has begun. By then it's too late.
As an alternative to setting the value in the code behind, you could use this quasi-declarative technique:
<asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" />
<%
btn.OnClientClick = <%=AlertMe%>;
%>
There are other ways to get around it. This answer to a similar question discusses several approaches you can take. I can't find any "official" reference (eg, msdn) that mentions this specifically, but I didn't look that hard. Here are a couple of blog posts that discuss the issue as well:
Blog post by Jim Black: Server tags cannot contain <% ... %> constructs
Blog post at weblogs.asp.net: Use ExpressionBuilder To Avoid “Server tags cannot contain <% … %> constructs”
Or google it with bing for more fun reading.
For anyone else encountering this issue, I ran across this blog from way back when:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
I found the sections about Expression Builders and ViewState concerns to be particularly informative.
I think its a quoting problem. Try
OnClientClick="<%=AlertMe%>"

Why does this line of localization behave this way?

I'm localizing an ASP.NET web site. Usually to localize text in an .aspx page I just use
<%= Resources.ResourceFile.ResourceName %>
For asp.net controls, this won't work. I have to use the syntax
<%$ Resources:ResourceFile, ResourceName %>
However, if I have a button and localize the Text property that way, but add any additional characters after it, the localization breaks and it shows as plaintext.
So Text="<%$ Resources:ResourceFile, ResourceName %> »" displays as
<%$ Resources:ResourceFile, ResourceName %> »
I'm sure there is a valid reason for this, I just can't find the explanation on MSDN on how the Text property evaluates this. I'm not even 100% sure on what the <%$ actually does.
What's happening is that ASP.net is invoking an Expression Builder. What effectively happens here is that rather than the ASP.net compiler translating your:
<asp:AControlWithATextProperty runat="server" Text="Some Text">
to:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = "Some Text";
When it transforms the markup in the .aspx file into a .cs file combined with the code-behind, it actually does something similar to this:
<asp:AControlWithATextProperty runat="server" Text="<%$ Resources:ResourceFile, ResourceName %>">
Becomes:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename");
It would seem that the asp.net compiler can't handle concatenating the content of the <%$ %> tags with any further text in the property from markup. Either a bug, or by design. i.e. You don't end up with ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename") + "»".
You can read more about the ResourceExpressionBuilder on msdn, ExpressionBuilder in general, or if you really want to; an implementation of one for localisation (database backed, hence the fact that I didn't use the ResourceExpressionBuilder) on my blog (3 parts).

Using delegates to render html blocks in asp.net

I thought of a neat hack this morning, probably not original, but I haven't seen it before. I don't consider it good practice, but can help when you need to render a block of code repeatedly around your page, and don't want to touch other code or create other files (partial views or components).
In your .aspx file create an anonymous delegate like so:
<%
Action<DataType> renderMe = data => {
%> Some html text That can contain quotes, etc.
And other <%= data.something %> stuff...
<%
};
%>
Then you can simply use it anywhere you want: (myvar1 and myvar2 are of type DataType)
This is some html and I want the block here: <% renderMe(myvar1); %> ...
or maybe here <% renderMe(myvar2); %>
I know it's not a great idea, but can anyone see any problems with doing this?
This ain't bad per se. Quite similar to Spark's macros (that looks better imho). Rashud`s script manager (for advanced js initialization) uses the same technique. MvcContrib's grid renderer does the same too.
Thing is - niche when this is suitable is really narrow. Only when desired snippet of html should be passed to server side or when you want to re-use it twice or more in context of one specific view but don't want to create separate partial view.

Categories

Resources