aspx .net vb simple basic codebehind example
Account Home | Help | Blog | Contact us | Log Out


Welcome to Kbytes > Articles

aspx .net vb simple basic codebehind example

Posted By: siteadmin on 01/04/2013 17:45:00

We were trying to use the example from http://support.microsoft.com/kb/312311 which appears in Google when you search for these terms.

The error returned was:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30451: 'Label1' is not declared. It may be inaccessible due to its protection level.
 
The Microsoft article has a few "gotchas" that we found.
 
The "Codebehind" parameter is not use for on-the-fly compliation, the "Codefile" parameter should be used instead.
 
However the main problem seems to be the use to of the SRC attribute in the Page Directive.
 
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SrcSample.aspx.vb" Inherits="CodeBehindSamples.SrcSample" Src="SrcSample.aspx.vb"%>
 
So the direct should be:
 
<%@ Page Language="vb" AutoEventWireup="false" CodeFile="SrcSample.aspx.vb" Inherits="SrcSample" %>
 
Since we are using NET 4.0 not 1.1 as was current when the article was written.
 
So....
 
http://msdn.microsoft.com/en-us/library/vstudio/ydy4x04a(v=vs.100).aspx   tells us that the SRC attribute in the Page Directive....
 
"Src
 
Specifies a path to a source file containing code that is linked to the page. In the linked source file, you can choose to include programming logic for your page either in a class or in code declaration blocks.
 
You can use the Src attribute to link build providers to the page. For more information, see the BuildProvider class. Also, in versions of ASP.NET prior to 2.0, the Src attribute was used as an alternative way to link a code-behind file to a page. In ASP.NET 2.0, the preferred approach to linking a code-behind source file to a page is to use the Inherits attribute to specify a class, along with the CodeFile attribute to specify the path to the source file for the class."

So to for a hello world of code behind use these two files:

SrcSample.aspx:

 

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="SrcSample.aspx.vb" Inherits="SrcSample" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    
    </div>
    </form>
</body>
</html>
 
and SrcSample.aspx.vb:
 
Public Class SrcSample
Inherits System.Web.UI.Page
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "(Src): Page_Load fired!"
 
End Sub
 
End Class

blog comments powered by Disqus

Kbytes Home | Privacy Policy | Contact us | Testing Area

© 2004 - 2024 1 Oak Hill Grove Surbiton Surrey KT6 6DS Phone: +44(020) 8123 1321