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 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....   tells us that the SRC attribute in the Page Directive....
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:



<%@ Page Language="vb" AutoEventWireup="false" CodeFile="SrcSample.aspx.vb" Inherits="SrcSample" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
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