i'm new to stackoverflow and developing web applications using Microsoft asp.net. I am using MS Visual Studio Professional 2015 as development tool.
What i am trying to do is create a simple web application for learning purposes. I have three files:
- WebForm1.aspx
- WebForm1.aspx.vb
- Code.vb (class file in subfolder "Code" in Visual Studio 2015 solution explorer)
My idea is to store all Subs an Functions that are used by multiple aspx files in the central class file called Code.vb. On my aspx i have a button and a Label:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Hello World" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
OnClick Event of Button1 calls Button1_Click from code-behind WebForm1.aspx.vb, which imports the central code class file:
Imports WebApplication1.CentralCode.Code
Public Class WebForm1
Inherits System.Web.UI.Page
Public Sub Button1_Click(sender As Object, e As EventArgs)
SetLabelText()
End Sub
End Class
In this class file, the Sub SetLabelText should set the Label-text of Label1 to "some text".
Namespace CentralCode
Public Class Code
Public Shared Sub SetLabelText()
WebApplication1.WebForm1.Label1.Text = "some text"
End Sub
End Class
End Namespace
The line WebApplication1.WebForm1.Label1.Text = "some text" is red underlined with error message "WebApplication1.WebForm1.Label1" is "Protected" and not accessible in this context and "WebForm1" is a class type and can not be used as Expression
Is it possible at all to do something like that? It would be much easier for me because my code could be stored in one central place.
Thx in advance, Markus