I don't know but this is a really simple error. When I create a hyperlink or any object in the html, and try to use it in the code behind, it says
name 'objectName' is not declared. When I try to use say a label in the codebehind lblTest, after I put a .(dot) after it, the property and methods list should pop up, but it doesnt since object is not recognized. I don't have a problem with
all the other objects that were previously created, just the ones that I add now aren't being recognized. In the previous versions of visual studio, the controls were added at the top after the line:
Inherits System.Web.UI.Page
For example, if I had a label control in the html part named lblTest, it would place the following line in the code behind:
Protected WithEvents lblTest
As System.Web.UI.WebControls.Label
However, this is not seen in the 2005 version. Is that the reason the conrol is not being recognized?...Do I have to explicitly place these lines of code for every control I add to the page? It doesnt seem like it since all the other controls that I previously
created on this page don't have these lines of code and I can access them. Thanks for your help,
VS 2005 uses partial class model one of the benefits of which is that you don't have to declare controls twice. It is sufficient to declare control in markup and declaration will be generated automatically. You don't see generated code, code file only contains
'user code'. It is difficult to say what is wrong, but please verify that
Everything was working fine and I could access anything, but I added another link within the repeater control, which I've marked in bold:
<asp:hyperlink
ID="hpSubTitles"
runat="server"
ForeColor="#0000ff"
Text="Click here to see more documents and resources for this lesson"></asp:hyperlink>
In the code benind, this is the only one I can't access (it gives me error saying hpSubTitles is undeclared. The code behind is as follows:
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.IO
Public
Class LessonPlan
Inherits System.Web.UI.Page
Dim countViewFeedback, countSubLessons
As Integer
Dim fb As
New objFeedback.feedback
Dim lp As
New objLessonPlan.lesson_plan
#
Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()>
Private
Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender
As System.Object,
ByVal e As System.EventArgs)
Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#
End Region
Private
Sub Page_Load(ByVal sender
As System.Object,
ByVal e As System.EventArgs)
Handles MyBase.Load
Dim course_id
As Integer
course_id =
CType(Request.Params("course_id"), Int32)
Dim myCourse
As objLessonPlan.lesson_plan =
New objLessonPlan.lesson_plan()
If Not Page.IsPostBack
Then
Then I have the following procedure defined in which I'm trying to access the hpSubTitles along with other code that I didn't post since it's long and not relative to the error I'm getting. I'm
just trying to set up the navigateUrl property for the hpSubTitles as shown below, but it doesn't let me.
Public Sub lesson_plan_onItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
hpSubTitles.navigateUrl = "SubLessons.aspx?lesson_plan_id=" + lesson_plan_id.ToString
End Sub
This looks like VS 2003 style code behind file. Did you migrate your Web before using it in VS 2005?
First I notice that Partial keyword is missing before the class declaration. Partial is very important as it tells compiler that some declarations and code will be coming from elsewhere (such as from markup). Without partial
control declarations won't be picked by intellisense. Here is how class looks when I create a new Web Form in VS 2005
Partial
Class LessonPlan
Inherits System.Web.UI.Page
End
Class
I think adding partial will fix the intellisense problem, but I would recommend migrating Web site to the VS 2005 model or installing Web Application Project add-on as there might be other issues as well. For instance, InitializeComponent
is no longer used.
I actually created this application in 2005 version, and I changed the class to have Public because I was trying to test something. However, even though I had partial class, it still does not recognize the controls.
Yes, any new page I create lets me access the controls and brings up the intellisense. It's just with any new controls I add on a current page. It's also weird that it works when I make the class Public and add the following line of code after the
Inherits System.Web.UI.Page
Protected WithEvents hpSubTitles
As New System.Web.UI.WebControls.HyperLink
If you add explicit declaration, it will definitely work, but that is what partial class architecture is supposed to do. VS 2005 does not support code behind model using full classes (at least not out of the box - you'll need Web Application Project add-on).
I would recommend creating a new Web for with code separation and moving code and element to the new page. Remember, you don't have declare them and you don't need initialize component and no need to add META elements from VS 2003.
inkaln
Member
533 Points
237 Posts
Error: name 'lblSubTitle' is not declared
Mar 28, 2006 05:37 PM|LINK
Hi,
I don't know but this is a really simple error. When I create a hyperlink or any object in the html, and try to use it in the code behind, it says name 'objectName' is not declared. When I try to use say a label in the codebehind lblTest, after I put a .(dot) after it, the property and methods list should pop up, but it doesnt since object is not recognized. I don't have a problem with all the other objects that were previously created, just the ones that I add now aren't being recognized. In the previous versions of visual studio, the controls were added at the top after the line:
Inherits System.Web.UI.Page
For example, if I had a label control in the html part named lblTest, it would place the following line in the code behind:
Protected WithEvents lblTest As System.Web.UI.WebControls.LabelHowever, this is not seen in the 2005 version. Is that the reason the conrol is not being recognized?...Do I have to explicitly place these lines of code for every control I add to the page? It doesnt seem like it since all the other controls that I previously created on this page don't have these lines of code and I can access them. Thanks for your help,
Mikhail Arkh...
All-Star
33139 Points
6083 Posts
Microsoft
Re: Error: name 'lblSubTitle' is not declared
Mar 28, 2006 11:52 PM|LINK
VS 2005 uses partial class model one of the benefits of which is that you don't have to declare controls twice. It is sufficient to declare control in markup and declaration will be generated automatically. You don't see generated code, code file only contains 'user code'. It is difficult to say what is wrong, but please verify that
1. Control has runat="server".
2. Control ID is correctly spelled.
3. Control is in the <form runat="server">.
------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
inkaln
Member
533 Points
237 Posts
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 03:21 PM|LINK
Hi,
I've checked all the points that you mentioned and I have those set up correctly. Any other ideas as to what might be causing this? Thanks
Mikhail Arkh...
All-Star
33139 Points
6083 Posts
Microsoft
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 05:08 PM|LINK
------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
inkaln
Member
533 Points
237 Posts
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 05:48 PM|LINK
Hi,
HTML code is as follows:
<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="LessonPlan.aspx.vb" Inherits="LessonPlan" %><
html><
head><
title>LessonPlan</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"></
head><
body><
h1 align="center"><b>Lesson Plans</b></h1> <h4 align="center">Click on the Link under the Title to view its Lesson Plans</h4> <H5></H5> <hr align="center" width="100%" color="#cc9900"> <form id="Form1" method="post" runat="server"> <asp:repeater id="repeaterLessonPlans" runat="server" OnItemDatabound="lesson_plan_onItemDataBound" OnItemCommand="lesson_plan_onItemCommand"> <ItemTemplate> <h5>Title</h5> <asp:LinkButton CommandName = "incrementCounter" id="lblTitle" ForeColor = "#0000ff" Text = '<%# DataBinder.Eval(Container.DataItem, "title")%>' runat="server" /> <br /><br /> <asp:hyperlink ID="hpSubTitles" runat="server" ForeColor="#0000ff" Text="Click here to see more documents and resources for this lesson"></asp:hyperlink> <h5>Description</h5> <asp:Label id="lblDescription" ForeColor = #cc6600 Text = '<%# DataBinder.Eval(Container.DataItem, "description")%>' runat="server" /> <br/><br /> <b> <asp:HyperLink ID="lblFeedback" runat="server" ForeColor="#0000ff" Text="Give Feedback"></asp:HyperLink></b> <b><asp:HyperLink ID="lblViewFeedback" runat="server" ForeColor="#993300" Text="View Feedback"></asp:HyperLink></b> <asp:Label visible = false id="lesson_plan_id" Text = '<%# DataBinder.Eval(Container.DataItem, "lesson_plan_id")%>' runat="server" /> <h5>This Lesson Plan has been viewed <asp:Label ID = "lblCounter" ForeColor = #cc6600 Text = '<%# DataBinder.Eval(Container.DataItem, "counter")%>' runat="server" />times
</h5> <hr width="100%" color="#006600" size="2"> </ItemTemplate> </asp:repeater></form></
body></
html>Everything was working fine and I could access anything, but I added another link within the repeater control, which I've marked in bold:
<asp:hyperlink ID="hpSubTitles" runat="server" ForeColor="#0000ff" Text="Click here to see more documents and resources for this lesson"></asp:hyperlink>
In the code benind, this is the only one I can't access (it gives me error saying hpSubTitles is undeclared. The code behind is as follows:
Imports
System.DataImports
System.Data.SqlClientImports
System.IOPublic
Class LessonPlan Inherits System.Web.UI.Page Dim countViewFeedback, countSubLessons As Integer Dim fb As New objFeedback.feedback Dim lp As New objLessonPlan.lesson_plan#
Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer.<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.InitializeComponent()
End Sub#
End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim course_id As Integercourse_id =
CType(Request.Params("course_id"), Int32) Dim myCourse As objLessonPlan.lesson_plan = New objLessonPlan.lesson_plan() If Not Page.IsPostBack ThenrepeaterLessonPlans.DataSource = myCourse.GetAllLessonPlans(course_id)
repeaterLessonPlans.DataBind()
End If End SubThen I have the following procedure defined in which I'm trying to access the hpSubTitles along with other code that I didn't post since it's long and not relative to the error I'm getting. I'm just trying to set up the navigateUrl property for the hpSubTitles as shown below, but it doesn't let me.
Public Sub lesson_plan_onItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
hpSubTitles.navigateUrl = "SubLessons.aspx?lesson_plan_id=" + lesson_plan_id.ToString End SubMikhail Arkh...
All-Star
33139 Points
6083 Posts
Microsoft
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 06:37 PM|LINK
This looks like VS 2003 style code behind file. Did you migrate your Web before using it in VS 2005?
First I notice that Partial keyword is missing before the class declaration. Partial is very important as it tells compiler that some declarations and code will be coming from elsewhere (such as from markup). Without partial control declarations won't be picked by intellisense. Here is how class looks when I create a new Web Form in VS 2005
Partial
Class LessonPlanInherits System.Web.UI.Page
End
ClassI think adding partial will fix the intellisense problem, but I would recommend migrating Web site to the VS 2005 model or installing Web Application Project add-on as there might be other issues as well. For instance, InitializeComponent is no longer used.
------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
inkaln
Member
533 Points
237 Posts
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 06:45 PM|LINK
Hi,
I actually created this application in 2005 version, and I changed the class to have Public because I was trying to test something. However, even though I had partial class, it still does not recognize the controls.
Mikhail Arkh...
All-Star
33139 Points
6083 Posts
Microsoft
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 08:26 PM|LINK
That's odd since only VS 2003 adds these:
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"><meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
Anyway, try adding a new, empty, default page, drop a button on it and see if it is available for intellisense in the code file.
------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
inkaln
Member
533 Points
237 Posts
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 09:04 PM|LINK
Yes, any new page I create lets me access the controls and brings up the intellisense. It's just with any new controls I add on a current page. It's also weird that it works when I make the class Public and add the following line of code after the Inherits System.Web.UI.Page
Protected WithEvents hpSubTitles As New System.Web.UI.WebControls.HyperLinkMikhail Arkh...
All-Star
33139 Points
6083 Posts
Microsoft
Re: Error: name 'lblSubTitle' is not declared
Mar 29, 2006 09:34 PM|LINK
If you add explicit declaration, it will definitely work, but that is what partial class architecture is supposed to do. VS 2005 does not support code behind model using full classes (at least not out of the box - you'll need Web Application Project add-on). I would recommend creating a new Web for with code separation and moving code and element to the new page. Remember, you don't have declare them and you don't need initialize component and no need to add META elements from VS 2003.
------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.