We help IT Professionals succeed at work.
Get Started

Can't access variable in code behind page

Mutsop asked
Last Modified: 2012-06-27

I have a project I'm working on, and I want to simply change a txtlabel on a page (which will be shown in a contentplaceholder). But for some magical reason, I can't access it.

Why do they make it so difficult?!

Here is the code
<%@ Page Language="vb" title="Homepage" MasterPageFile="~/MasterPage.Master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<asp:Content ID="ContentHomePage" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    This is the HomePage
    <asp:Button ID="Button1" runat="server" Text="Button" />

Open in new window

Public Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub Button1_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.text = "hi"
    End Sub
End Class

Open in new window

I get 2 errors in the page behind:
Error      97      Handles clause requires a WithEvents variable defined in the containing type or one of its base types.      C:\***\WebApplication1\WebApplication1\Default.aspx.vb      7      94      C:\...\WebApplication1\

Error      98      Name 'Label1' is not declared.      C:\***\WebApplication1\WebApplication1\Default.aspx.vb      8      9      C:\...\WebApplication1\

When I check other examples, you don't have to declare any labels again in the code behind...What am I doing wrong?
Watch Question
This problem has been solved!
Unlock 1 Answer and 18 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE