ASP.net vb code behinddeployed compiling error - asp.net

Background: I am not an ASP developer, I understand OOP processes and can program in Javascript, Actionscript, and am learning PHP. I used to know VB.net in a limited fashion.
I am forced(by existing platform configuration) to develop a small .aspx page, i have been reading many tutorials, but am unable to get my page below to work. The best i can get is an 500 error message but no details are listed even though detailed error messages is configured. I dont have access to Visual Studios for this an am using Dreamweaver to develop.
I'm attempting to use the compiling method that compiles when the page is first viewed instead of precompiling
I cant seem to get this basic "hello world" type page to work. What am i doing wrong?
Edited code to represent changes made
CurrentNews.aspx
<%# Page Language="vb" AutoEventWireup="false" Src="/Scripts/CurrentNews.aspx.vb" Inherits="NewsFunctionality" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>News</title>
<link rel="stylesheet" type="text/css" href="CSS/NewsLayoutOne.css"/>
</head>
<body>
<div class="OuterDiv" runat="server">
<img id="NewsImage" src="Images/DefaultNews1.png" >
<div id="NewsBody" runat="server">Original Text</div>
</div>
</body>
</html>
CurrentNews.aspc.vb
' VB Document
Class NewsFunctionality
Inherits System.Web.UI.Page
'On load event for the page linked to this class file
protected sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
NewsBody.InnerHTML = "Dynamic Text Generated."
End Sub
End Class

From a quick glance, i would say at minimum your code would not do what you expect. You are putting "<P>Dynamic Text Generated</P>" to be rendered in an input field.
Other than that though im not seeing anything that obviously would not work. What is the behavior you see?

Your Page_Load sub should declared protected. The relation between aspx page and aspx.vb class is somehow like inheritance.

Related

First link to internal page works. Subsequent attempt causes Http 404 Error in ASP.net 2.0

I am using ASP.net 2.0 in VS 2008 with Framework 3.5, VB.net codebehind
I plan to build a website that would have a stationary menu on the left made with a TreeView in a fixed position div. A second div on the right would be for content. I have made a small test app to see if the concept works using Master / Content pages.
I want the menu to be hard coded in a single place. The Master page works well for that. The idea is to not have to edit the menu in separate pages as the site grows.
The test app has one Master page and one Contents page. That is how I would like to set up the actual website. The Contents page is the start up page. The Master page has the menu items. Each menu item links to Contents.aspx, but with a unique querystring or /keywords appended. The Contents page codebehind Load event reads the Request.Url.AbsoluteUri. Then, based on the appended querystring or /keywords, pulls the content data and pushes it to an empty Literal control on the Contents.aspx page. In this test app I just push plain text for simplicity.
This works well until I re-click a link. That causes an Http 404 error. I have tried using Response.TrySkipIisCustomErrors = True at the top of the Master and Content Load events and that did not help. I can click one menu link several times and the page will keep reloading. But going back and forth between the links causes the error.
When the error occurs it happens before the Contents.aspx Load event starts.
I would like to be able to fix this in a way that will work on an asp.net host site.
MasterPage.master:
<%# Master Language="VB" CodeFile="MasterPage.master.vb"
Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div style="position:fixed; top:0px; left:0px; width:300px; height:100%;
background-color:#ddd; padding-top:10px; padding-left:10px" >
Home<br />
<a href="Contents.aspx?p=ABCD" >ABCD page</a><br />
<a href="Contents.aspx/?p=WXYZ" >WXYZ page</a><br />
</div>
<div style="position:absolute; top:0px; left:310px; width:40%;
height:100%; background-color:#eee; padding-top:10px; padding-left:10px" >
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Contents.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Contents.aspx.vb"
Inherits="Contents" MasterPageFile ="~/MasterPage.master" title="Contents"%>
<asp:Content ID="Contents" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Literal ID="Contents_Main" runat="server">
</asp:Literal>
</asp:Content>
Contents.aspx.vb codebehind:
Partial Class Contents
Inherits System.Web.UI.Page
Public Sub Contents_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
Try
Dim url As System.Uri = Request.Url
Select Case True
Case url.AbsoluteUri.EndsWith("?p=ABCD")
Me.Contents_Main.Text = "content for ABCD"
Case url.AbsoluteUri.EndsWith("?p=WXYZ")
Me.Contents_Main.Text = "content for WXYZ"
Case Else
Me.Contents_Main.Text = "Landing Page"
End Select
Catch ex As Exception
Me.Contents_Main.Text = ex.Message & ex.StackTrace
End Try
End Sub
End Class
I was able to solve this by using the same concept in a single aspx form, rather than Master/Content. The menu is now in that single form and the codebehind pulls the data for the content. I do not know what was causing the Http 404 error, but using the single aspx form resolved it. Also, the URL must include a querystring, in the form of www.example.com?s=values, not www.example.com/keyword1-keyword2. The latter syntax also threw the 404 error.

CS0103: The name 'rand' does not exist in the current context

I know this error has been the center of about a dozen questions, but I'm not seeing my situation in any of those other questions.
I have the following at the top of an aspx page:
<% String rand = new Random().Next().ToString(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="../js/script.js?v=<%=rand%>"></script>
It builds just fine, but when the page is accessed, I get the error complaining about the <%=rand%> part of the line saying it doesn't know what rand is. As a large web application, we're not wanting to cache code that changes frequently and this is the approach we're taking. In fact, this exact approach works on another page of ours but is not working on this page for some reason. We want to only generate one random number because it's used across multiple js files that we don't want cached.
I've looked through many of the other CS0103 questions but none of them I read talk about variables created in the markup of the aspx page.
As an alternative solution...
<script type="text/javascript" src="../js/script.js?v=<asp:Literal id="randNum" runat="server" />"></script>
And then in your code behind.
randNum.Text = new Random().Next().ToString();
As a side note though, using this method to prevent caching is a bit of a hack. If you are using .NET 4.5, I would suggest looking into the scriptBundle and styleBundle classes, which essentially implement this methodology as well as minify your scripts/css.

include style sheets in all pages asp vbscript

How can I include pages, style sheets, or links to them, automatically into my ASP VBscript pages? I read something about 'global' pages, but I am unsure what they mean and how it is that I can accomplish such a thing. I'm sure this is an easy question, but it's of great help to me as I've been writing VBscript for 2 days now! I'm not exactly an expert on HTML in general either, but I feel I have a reasonably good grasp of things. I would appreciate a good detailed example of how a 'global' page plays with my other ASP pages.
I'm setting up my first site...a management site for the main site I intend to build afterward. I want to get all my ducks in a row before moving forward with the public site. Can someone please give me some detailed information on how to include these pages/links automatically (page includes(header/footer), style sheets, etc) globally throughout my site without the need of using <!--#include file.... on each page I make, because that is kind of a pain and I'm sure there is an easier way. If there is, I know you can help! Thanks in advance, I look forward to hearing what options/possibilities are available.
If you insist on using ASP Classic you may find some method for handling masterpage like functionality but it is, to the best of my knowledge, not suppoerted as such by the framework.
[Edit] Given the edit of the original question the method first demonstrated is not so interesting, hence I suggest an alternative method too.
You could make a general ASP-page which serves all traffic to the site. A queryparameter then specifies which subpage should be displayed. Subpages are made as seperate ASP-pages which are executed by the general/master page or by another subpage. A very crude example of this could look like this:
<%
url = Request.QueryString("url") & ""
if url = "/" or url = "" then
subpage = "home.asp"
else
subpage = url & ".asp"
end if
%>
<!DOCTYPE html>
<html>
<head>
<title>Header for all pages</title>
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
<% Server.Execute(subpage) %>
</body>
</html>
The site should then be addressed in this fashion:
www.domain.com/default.asp?url=/contact
which would load the contact.asp subpage into the masterpage or:
www.domain.com/default.asp?url=/user/1234/profile
to load a user's profilepage (displayed by the profile.asp in the folder user/1234). This last example raises some issues because then every user has to have a folder containing all the asp-files (which is far from optimal) so you might want to employ some interpretation of the url queryparameter to redirect input in a more intelligent way.
Another issue is the fact that subpages are ASP-pages themselves which means someone could reference them directly. This calls for some action to protect those subpages from direct reference. It can be done but this would probably mean including some code => back to square one!
Another disadvantages of this approach is that subpages are rendered in their own context and hence can't access functionality and data from the calling page's context. This means that global data has to be shared in some other way (session, application, database or some other way). Data can't be passed to the subpage either (and no, Server.Execute doesn't allow query-parameters).
The include-way
Personally I think you get the most flexibility by using header/footer includes as demonstrated in my original post and shown below.
One way is to put your general stuff in includes and then includes those bits on each ASP-page. E.g.:
<!-- #include virtual="/includes/header.asp" -->
content goes here
<!-- #include virtual="/includes/footer.asp" -->
And your header.asp could look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Header for all pages</title>
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
and footer.asp like so:
</body>
</html>
This strategy has some disadvantages. The header is fairly static which could present some problems with SEO; For one the title should fit the pagecontent which is hard to accomplish when the include contains the header-markup. This could be facilitated by some global variables that are set prior to the include-section i.e.:
<%
title = "Title for this page's content"
%>
<!-- #include virtual="/includes/header.asp" -->
content goes here
<!-- #include virtual="/includes/footer.asp" -->
and then in the header like so
<!DOCTYPE html>
<html>
<head>
<title><%=title%></title>
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
but that already begins to "smell" a little because you set up some expectations for the including page inside the include-file. At least you have to be very disciplined when constructing your pages.
The term you're looking for is Master Page, not Global Page, that may be why you're having a hard time finding what you're looking for on Google. Basically consider a master page a template. You create a master page, then load other pages into it. There are content place holders that you put in the master then populate on your other pages.
So a very basic example would look something like this.
<%# Master Language="VB" CodeFile="general.master.vb" Inherits="master1_general"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<link rel="stylesheet" type="text/css" href="/styles/main.css?v2"/>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder id="body" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
Then your individual pages would look like this:
<%# Page Language="VB" MasterPageFile="~/master/general.master" AutoEventWireup="false" CodeFile="base.aspx.vb" Inherits="_Default" title="Opportunities" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
//any additional head stuff specific to this page goes here.
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server" >
//your body mark up goes here.
</asp:Content>
Notice how the Master page is actually a web page. Then it has place holders in certain spots. In this one there is a place holder in the head and one in the body. Then on individual pages I identify which master page to use and what data (if any) goes in the place holders. I always include a placeholder in the head so I can load js or resources that specific pages need on that page only.
Then the individual pages are just the content that goes in the placeholders.

Could not load type 'ASP.xxx' when referencing a precompiled master page

I'm attempting to precompile a few master pages (not update-able) to share them across multiple applications. The project I'm precompiling is a Web Site. The project that references precompiled assemblies is a Web Application. However, I'm getting a Could not load type 'ASP.xxx_master' every time i try to reference the master page from the client.
<%# Master Language="C#" Inherits="ASP.sitebase_master" %>
My precompiled master page looks like this.
<%# Master Language="C#" ClientIDMode="Static" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="AspNetHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=7" /><![endif]-->
<asp:ContentPlaceHolder ID="MetaContent" runat="server" />
<title>Web Portal</title>
<link href="/media/css/style.css" rel="stylesheet" type="text/css" />
<link href="/media/js/plugins/colorbox/colorbox.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="StyleContent" runat="server" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" language="javascript"></script>
<script src="/media/js/plugins/colorbox/jquery.colorbox-min.js" type="text/javascript" language="javascript"></script>
<script src="/media/js/plugins/filestyle/jquery.filestyle.min.js" type="text/javascript" language="javascript"></script>
<script src="/media/js/portal.master.js" type="text/javascript" language="javascript"></script>
<script language="javascript" type="text/javascript">
PORTAL.debug.init();
PORTAL.init();
</script>
<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
<div id="hld">
<div class="wrapper">
<form id="AspNetForm" runat="server">
<asp:ContentPlaceHolder ID="BodyContent" runat="server" />
</form>
<asp:ContentPlaceHolder ID="FooterContent" runat="server" />
</div>
</div>
</body>
I'm stumped. No idea why the type isn't resolved. Anybody got suggestions? Both projects (precompiled web site and client web application) are built for ASP.NET 4.0.
EDIT: Here is the list of dependencies of the precompiled assembly. No 3rd party references.
mscorlib,
System,
System.Web
UDPATE 1
Well, the quick fix to this issue is to specify the full path to the master page.
<%# Master Language="C#" Inherits="ASP.sitebase_master, App_Web_sitebase.master.cdcab7d2" %>
After doing that, I'm receiving the following error:
An error occurred while try to load the string resources (FindResource failed with error -2147023083).
After doing some research, this appears to be related to the way HTML markup is parsed within the master page. Not entirely sure yet. I haven't dug much deeper into it. Overall, I can't believe this is the recommended way to share controls as it is absolutely, mindbogglingly idiotic.
UPDATE 2
I couldn't make anything of value out of this. It appears to be hating "script" tags in the head section, but I don't know why. The Master Page works great with a single script include. As soon as i start adding more I keep getting that error. After wasting a full day on this I ended up submitting a bug report to Microsoft. If anyone wants to bump it, please do.
UPDATE 3
I spent a few more days debugging this after no response from MS. Here is my findings. I initially thought that the code generated by CodeDOM provider is looking for a .NET resource that somehow did not get embedded in the assembly when it was published. I was wrong. After some investigation it appears that what's happening is after the Master Page reaches a certain size, a chunk of it is stored in the Resource Table in PE Data Directories section of the assembly. In fact, after looking at the generated assembly in PE resource viewer, i was able to confirm this by finding all my script includes in the Resource Table. Now, here is the actual problem. What's happening is that the CodeDOM provider generates a call to Win32 FindResource to pull that resource from the Resource Table. However, FindResource doesn't work on assemblies in memory, only on disk. So it fails with the above exception. I'm getting close, but still no workaround.
I finally have a workaround. It's not pretty, but it resolves the issue. Apparently using LoadControl to pre-load precompiled MasterPages loads all of the resources that FindResource cannot find otherwise. So, here is all i did to make this work.
In my client application I created a dummy master page (i.e. Dummy.Master) which references my precompiled master page like so:
<%# Master Language="C#" Inherits="ASP.sitebase_master, App_Web_sitebase.master.cdcab7d2" %>
Now, any .aspx page which references Dummy.Master needs to preload the precompiled MasterPage type like so:
protected override void OnPreInit(EventArgs e)
{
ASP.sitebase_master mp = (ASP.sitebase_master)Page.LoadControl(typeof(ASP.sitebase_master), null);
base.OnPreInit(e);
}
I don't know why this works, but it does. This code must run before the MasterPage is resolved, so PreInit worked great. After glancing at the .NET code for a few seconds in Reflector, it appears that LoadControl actually does some assembly compiling voodoo when it attempts to load a certain control type. So perhaps something in there loads that PE resource data section. The best place to put it would be in the base class I suppose which all pages could inherit from. Also, each loaded control (master page in this case) ought to be cached. Here is a good article explaining just that.
Hopefully this helps someone as much as it helped me. It was a pretty big show stopper for me.

Does Javascript in the html body execute when encountered?

I have inherited an ASP.net codebase and I have very limited ASP.net skills. I am struggling to understand why something works and also why it only works in IE.
The following code appears in a page :-
<%# Page Language="C#" AutoEventWireup="true" CodeFile="map.aspx.cs" Inherits="Romtrac.auth_map" Culture="auto" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>
<% = Resources.Resource.map %>
</title>
</head>
<body>
<form id="adminlw_map_form" action="<%=geturl()%>" method="post" >
<% = Resources.Resource.loading %>...
<textarea name="xml" id="xml" runat="server" style="display:none" cols="1" rows="1"></textarea>
</form>
<script language="javascript" type="text/javascript" >
//submit main form immediately
document.getElementById('adminlw_map_form').submit();
</script>
</body>
</html>
This code runs fine in ASP.net. The form is automatically submitted and the page returned is rendered correctly within an Iframe. My question is;
1) Does the javascript within the body just get executed when it is encountered? Is this good practice or should it be executed in response to an event?
2) Why does this not work in other browsers?
Yes
The javascript is being executed before the browser has fully rendered the page. In this case, the form has not been rendered and is not accessible via the DOM.
The execution needs to happen after the DOM is fully loaded by the browser and can be implemented by encapsulating the call within a function and calling that function via the onload event of the body or by using a javascript library like jquery to hook into the load event of the page.
1) Yes, No. jQuery does it best with it's $(document).ready() function, but you should wait for the page to finish loading before running Javascript.
2) Do #1 and you won't need to worry about this. However I'll leave the floor open to someone with a better answer.
Some browsers prevent the submission of a form without user interaction in the page's load 9it's a security risk). I've had this issue a number of times in the past. I would combine the page's load, with a window.setTimeout of say 100ms.
<body onload="window.setTimeout( document.getElementById('adminlw_map_form').submit, 100)">
....
Remember when using JavaScript to keep it unobtrusive. There are still people out there who have JS switched off and your page should not really on it. It should serve as an enhancement.
Same as Mike Robinson's answer but surely you can just use <body onload="myfunction();">?

Resources