Conditional includes in ASP - asp-classic

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.

Related

Commenting code in 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%>

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.

How to fetch Title of news Article in GoogleNewsSitemap using kentico CMS

I am using Kentico 5.5R2 for my website, where i want to generate GoogleNewsSitemap. So far my code is able to fetch newarticle url and publication date, but i'm not able to fetch the title of news article dynamically
here's code
<url>
<loc><%# GetAbsoluteUrl(GetDocumentUrl()) %></loc>
<news:news>
<news:publication>
<news:name>Yourwellness </news:name>
<news:language>eng</news:language>
</news:publication>
<news:genres>PressRelease, Blog</news:genres>
<news:publication_date>
<%# GetDateTime("DocumentModifiedWhen", "dd-MMM-yy")%>
</news:publication_date>
<news:title>method for fetching title Comes here </news:title>
<news:keywords>Health</news:keywords>
</news:news>
</url>
I tried many methods to fetch title but it comes as blank.
<%# Eval("DocumentName") %> is the default for the node unless you are using something else with a custom Document Type
Have you tried just a regular Eval? Can't remember the field name, and not at my work computer, but probably one of these:
<%#Eval("DocumentTitle") %>
<%#Eval("Title") %>
You might be looking for:
<%# Eval("DocumentName") %>
It depends on what the individual Document Type is using for the title. It could be the document name as above, or something like "NewTitle".
Where are you using that code and what web part are you using?
Have you tried to get the title in the code behind?

accessing variables declared outside the asp code

<script ID="clientEventHandlersVBS" LANGUAGE="vbscript">
s=pass()
y=s
</script>
<%
session("password")=y
Response.write(session("password"))
Response.write(y)
%>
i have this code. but nothing is getting stored inside the session variable neither anything is getting printed. cant i access the variables declared outside the asp code or is their any syntax mistake. any help is really appreciated
First of all put
<% Option Explicit %>
at the top of every .asp page.
You will immediatly see that you are trying to access non declared variabels s and y.
So of course nothing is stored in the session variables.
Can you not use
<%
s = pass
y = s
%>
and so on ?
What is the purpose of the <script ... line if you are using vbscript any how ?
As implied by the ID of the script (clientEventHandlersVBS) the code contained in there refers to the client (the browser, IE in this case since it is the only one that supports VB client-side)
the <% %> tags though refer to server side ASP code..
These two can never communicate as they happen at different times/computers...

ASP.NET MVC Modify CSS links via code (runtime/development)?

does anyone know of a good way of doing the following, I need to have available via CSS Links up to 5 CSS files in one, this is pretty bad for trips to the server.. but it helps me keep it organized...
At runtime i automatically consolidate and minify all css files into 1 ...
What i was wondering is how to have links to css files in my source at design time and different at runtime??
Is there some special workaround i can do with <% %> tags?
Thanks
You could have one link to the combined file in your HTML and use a build event to combine your separate ones in to the one file. That way, during dev you always see your neat separate files but the designer (and at runtime) will always see the combined file. The designer doesn't care of there is one or 5 files but you do. This way you don't have to do any conditional design-time only logic and your code will be cleaner.
I use if (false) with HtmlHelper extensions to achieve similar effects. It might look like:
<% if (false) { %>
<link href="../content/styles/jquery-ui.css" ...
<link href="../content/styles/site.css" ...
<% } %>
<%= Html.CSS( "jquery-ui.css", "site.css", ... ) %>
You can try the following in your view or master page:
1) Leave the min'ed CSS links in as they are
2) Use this conditional block to include the CSS files directly as needed for design time:
<% if (this.DesignMode) { %>
<link rel="stylesheet" type="text/stylesheet" href="css/styles.css" />
<% } %>

Resources