Commenting code in ASP Classic - asp-classic

The way I know of hashing out code within ASP Classic is <%-- --%>. Would this be correct? Or is there another way?

Use a single quote, like:
' This is comment
ASP Classic uses the VBScript/Visual Basic language, and a single quote is commenting in that; <%-- is nothing (I am not 100% sure though).

Beside ', you can comment lines in the old school way:
REM Response.Write "Ignore this line"
Which is same with
' Response.Write "Ignore this line"

Assuming you mean that you have large block of inline code like the below you want to disable:
<%
CallSomething()
DoSomething()
Response.Write("all done")
%>
Then either comment out each line as described in this other answer or other approach is:
Create a dummy, empty file called "dummy.asp" and place it in the same folder.
Change the code block to this:
<script language="vbscript" runat="server" src="dummy.asp">
CallSomething()
DoSomething()
Response.Write("all done")
</script>
Note: you need to change only the <% and %>, all other lines can stay intact. Having a src in the script tag will cause the Classic ASP engine to take the file contents instead of taking the script block contents.
Then when you want to uncomment, either do it for each line or put back the <% and %>.

The question says... ASP classic.....
All the above answers are good, but specific to VBScript.
But a classic ASP file can also contain HTML and Javascript
Commenting VBScript code in a classic ASP file:
Refer any of the approved answers.
Commenting HTML code in a classic ASP file:
Refer https://www.w3schools.com/TAGS/tag_comment.asp
Commenting Javascript code in a classic ASP file:
Refer https://www.w3schools.com/js/js_comments.asp

This is the best way to comment out large blocks of code:
<%if 1=2 then%>
html or other code here
<%end if%>

Related

Conditional includes in ASP

We have a number of categories with products in an ASP eCommerce site and, based on the category, would like a particular file to be included. For example, if URL is:
viewPrd.asp?idproduct=6&idcategory=18
then based on idCategory=18 I would like to include:
<!--#include file="menu18.asp"-->
If idCategory=19 then I would like to include:
<!--#include file="menu19.asp"-->
and so on. Any assistance would be appreciated.
I use a script taken from https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9796&lngWId=4 (dynamic ASP inclusion)
So after including that script in your code, you just have to write:
myMenu = Request.Querystring("idCategory")
Include("menu"&myMenu&".asp")
Unfortunately, include files are a feature of IIS, not ASP. They are included before the page is sent to the ASP-processor that interprets your ASP code.
For this reason you can't have conditional includes.
The includes are already in place when the ASP code is executed.
To have conditional code in ASP you could use script components (WSC files), which you can include/load conditionally, or use big if...then or case.. constructions inside an include.
More info on the use of WSC's can be found here
As far i know its not possible on a "elegant way", but i suggest you to take a look in those links:
is it possible to issue dynamic include in asp-classic?
https://support.microsoft.com/en-us/kb/192144
Regards.
Yes, its possible. Just leave the include statements outside the <% %> code delimiters.
This is my quick and dirty solution and it works:
<% session("lang") = request.querystring("lang") %>
<%if (session("lang") = "es" or session("lang") = "") then %>
<!--#include file="espanol.asp"-->
<% else %>
<!--#include file="ingles.asp"-->
<% end if %>
<!--#include file="precios.asp"-->
IIS will include the two files, but ASP will use only the file that satisfies the if conditions.

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.

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

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 >.

How do i get rid of __o is not declared?

I have some code in my master page that sets up a a hyperlink with some context sensitive information
<%If Not IsNothing(Profile.ClientID) Then%>
<span class="menu-nav">
<a target="_blank"
href=
"http://b/x.aspx?ClientID=<%=Profile.ClientID.ToString()%>&Initials=<%=Session("Initials")%>"
>
Send
<br />
SMS
<br />
</a>
</span>
<%End If %>
<span class="menu-nav"> <!-- Name __o is not declared Error is flagged here-->
Now the issue seems to be in the href part. If I remove the dynamic code the error disappears. Can anyone tell me how to resolve this issue?
I've found the answer on the .net forums. It contains a good explanation of why ASP.Net is acting the way it is:
We have finally obtained reliable repro and identified the underlying issue. A trivial repro looks like this:
<% if (true) { %>
<%=1%>
<% } %>
<%=2%>
In order to provide intellisense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the intellisense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:
if (true) {
object #__o;
#__o = 1;
}
#__o = 2;
The workaround is to add a dummy expression early in the page. E.g. <%="" %>. This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential ‘if’ (or other scoping) statement.
An alternative solution is to simply use
<% response.write(var) %>
instead of
<%= var %>
Yes, I have experienced the same bug occasionally in pages that use server side constructs on ASPX pages.
Overtime, I found a fix for it (I'm sorry, I just haven't been able to find out where I found this bit of info again.) and that fix is to put the following code above the errant <%...%> block:
<%-- For other devs: Do not remove below line. --%>
<%="" %>
<%-- For other devs: Do not remove above line. --%>
Apparently, where you put the above code makes all the difference to VS.NET, so it may take a few tries to get it right.
This is an odd solution, but for me I managed to fix this problem by simply closing the offending open files in Visual Studio.
With them open, i was erratically getting the __o problem.
As soon as I closed them, the __o problem disappeared.
After some hours of googling and analyzing bunch of aspx'ses in my current project seems I've found the solution, that is working for me. Would to advise strongly avoid html-style comments:
<!-- ... -->
inside aspx page. Instead of it use aspx-style comments
<%-- ... --%>
Additionally it helped me obtain that vs intellisense and code highlighting became working again and the mainly thing - this case had begun from it - vs can now hit the breakpoints inside embedded pieces of vb/cs code! And no any damn "This is not a valid location for a breakpoint" message.
When I've cleaned the solution, restarted IIS and it is still mysteriously playing up, I find this can sometimes be caused by pasting an ASPX source file's contents from another system into Visual Studio which "helpfully" updates the code, possibly changing some IDs and breaking the page.
Pasting it into another editor (Notepad++?) then saving it stops Visual Studio from "being helpful" and the page works again.

What are these called <% ... %>? And what ways can they be used in a page

In a .Net web form...
What are these special tags called?
I know of 2: <%-- comment --%> for comments
and <%# used with Eval and Bind #>
So what does the # signify? Are there more?
I know you can put some basic logic and function calls in there but I've never found anything that really describes how that can be used and should be used.
I hope this isn't a duplicate but it's really hard to search for <%#
They're also called bee-stings:
In ASP.Net, what is the difference between <%= and <%#
Server tags. They are called server tags.
Of course there are more.
<%= "string constant" %> - it will output a given string to the HTML output
<%= BO.Customer.GetName () %> - will do the same with the function that returns a string result
<% RenderMyCoolControl %> - without the "=" character, it is supposed that your function will render something to the HTML output using Response.Write
Or you can use that directly: <% Response.Write ("string constant") %>
Hope that helps.
Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc
Are there more?
Take a look here:
Inline tags
There's a fairly complete list here:
ASP.NET "special" tags
And as the accepted answer on there states, they are "Server Side Scripting Tags"

Resources