Cannot access controls on aspx page from codebehind file

praveensg

Newcomer
Joined
Jan 23, 2004
Messages
23
Hello Friends,
This might sound really stupid but I'm runnin into this problem.

I have a dropdownlist in my aspx page which uses a .vb file. I'm trying to populate the dropdownlist in the Page_Load event that I've written in the codebehind file. However, it throws me an error saying Name not declared. Following are my files. Let me know what Im doing wrong.

Thanq
Praveen

Herez the .vb file
Code:
Imports System 
Imports System.IO 
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.Page

Public Class RegisterClass:Inherits Page

Public Sub Page_Load(Sender As Object,e As System.EventArgs)
Dim i as Integer
Dim DtNow As DateTime
Dim strConnection As String
strConnection = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")

ddlDay.Items.Add("Day")
.
.
.
.

The .aspx file

Code:
<%@ Page Language="VB" src="register.aspx.vb" Inherits="RegisterClass" ContentType="text/html" ResponseEncoding="iso-8859-1" %> 
<link rel="stylesheet" type="text/css" href="styles.css" /><html>
<head>
<title>Registration</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

.
.
.
.
.
.
<asp:dropdownlist ID="ddlDay" runat="server"></asp:dropdownlist>
 
Last edited:
The control isn't being defined within the codebehind. If you are using Visual Studio sometimes your controls don't get added to the codebehind until you view the page in Design View beforehand.

Visual Studio can be a real pain in the ***. You can also choose to define the control by hand, just add the following line to your codebehind:

Dim ddlDay as System.Web.UI.WebControls.DropDownList
 
No problem man. Even better yet, do what I do and use Visual Studio more like you would Notepad, only with intellisense, color coding, etc... but don't count on any of the designer-centric features to work all the time ;)
 
Back
Top