1
votes

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

2
Usually, access to ui element are kept within the class (here WebForm1 ) since they are private. In your Code class, you could have a method that returns the text you need to populate the label. If you just want to populate label, I would look at having a resource file. Are you having trouble with just to example? - the_lotus
thanks for your hints. solved the Problem now. - Markus S.

2 Answers

0
votes

The error message is clear: you cannot access Label1 from outside the form WebForm1.

I recommand that you change your sub to a function and return the text:

Public Shared Function GetLabelText() as String
Return "Some Text"
End Function

And in Webform1

Public Sub Button1_Click(sender As Object, e As EventArgs)
    Label1.Text = GetLabelText()
End Sub
0
votes

Don't.

To continue on this approach would be to tightly couple your logic with your user interface. That's only going to make things more difficult. Keep the page control interactions in the page's code-behind and have your business logic return the values which goes on the page.

So something like:

Public Shared Sub GetText()
    Return "some text"
End Sub

Then in your page's code-behind, you would set the label to the text you're getting:

Public Sub Button1_Click(sender As Object, e As EventArgs)
    Label1.Text = Code.GetText()
End Sub

The idea is that the "business logic" operates on just values and commonly-known classes. The "application logic" interacts with the business logic and connects it to the user interface. This keeps the two simpler and easier to support and maintain, easier to add features, more portable to other systems, etc.