locked
Declaration error error message for a label control that already exists? RRS feed

  • Question

  • User1080700311 posted

     


    Hello,

    I am fairly new to VB .net, and am having a strange error being given by the compiler.
    First I dragged and dropped a label control on my web form. But then whe I go to
    my VB .net code behind file, if I try and set a property on that label control, I am
    getting a "Declaration Expected" error message for the label I created.

    Yet if I do try and explicitly declare my label control, the compiler tells me that the label
    control is already declared. Does anyone know what the problem is here?

     

    Partial Class Coin
        Inherits System.Web.UI.Page

        Label1.Visible=false ' Declaration expected error here. Yet if I try to declare it, I am told that it is already declared
    End Class

     

     

     

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Coin.aspx.vb" Inherits="Coin" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
        </div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>

    Monday, May 25, 2009 4:37 PM

Answers

  • User849259894 posted

    You should do this inside a method (like below).

    Partial Class Coin
        Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
              Label1.Visible=false  
            End If
        End Sub
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 25, 2009 4:46 PM

All replies

  • User849259894 posted

    You should do this inside a method (like below).

    Partial Class Coin
        Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
              Label1.Visible=false  
            End If
        End Sub
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 25, 2009 4:46 PM
  • User1080700311 posted

    This font looks really small so I am going to bold my response so it can be seen. I appreciate the help. Any other ideas from anyone? 


    That did not work.

    I still get the error message
    C:\KOR\AddNotice.aspx.vb(8,0): error BC30451: Name 'Label1' is not declared.
    C:\KOR\AddNotice.aspx.vb(27,0): error BC30451: Name 'Label1' is not declared.

     

     

     

    Monday, May 25, 2009 7:15 PM
  • User1080700311 posted

    I hope someone can help me. I can't get this to work. I am under a tight deadling so if someone can help me I would be so thankful. Below is the code:

    Problem: 

    I drag and drop a label control onto my web form. Yet if I try and reference the label
    control in the code behind, I get a declaration expected error. If I then try to explicitly
    define it in my code behind file, I am told that the label is already declared. Please help
    me. This is very frustrating. I also placed comments next to my sample code below for
    further clarification.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Coin.aspx.vb" Inherits="Coin" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        </div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>


    Partial Class Coin
        Inherits System.Web.UI.Page
        Dim Label1 As Label  ' Label1 is already declared as 'Protected' with events Label1 As System.Web.UI.WebControls.Lable in this class
        Label1.Visible=false ' Declaration expected error here. Yet if I try to declare it, I am told that it is already declared
    End Class

    Tuesday, May 26, 2009 9:54 AM
  • User1080700311 posted

    actually code.asp is correct. Thanks!

    Tuesday, May 26, 2009 9:57 AM