Name is not declared

D

DC Gringo

I'm getting a BC30451: Name 'pageID' is not declared.

I have a user control that contains a simple condition to display one row or
another like:

<% If x = 1 Then %>
....some html...
<% Else %>
....some html...
<% End If %>

The control is used to in two different .aspx pages (e.g. first.aspx and
second.aspx). In each .aspx page, I've tried to set this variable right
after Private Sub Page_Load in the code-behind page to no avail...such as:

Public Class WebForm1
Inherits System.Web.UI.Page
Web Form Designer Generated Code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Dim x As Integer = 10000

End Class
 
M

Martin Dechev

Hi, DC Gringo

Although your design is wrong, the solution is the following:

Create a property x of type Integer in the class of the usercontrol. Also a
public field of the class will work, but it is one of the worst things you
can do in the OOP world.
Assign this property (or field) from the Page in the Page_Load method.

Now, the good design:

Put the part between <% If x = 1 Then %> and <% Else %> in one
Panel/PlaceHolder/whatever container control with runat="server" and the
part between <% Else %> and <% End If %> in another container control with
runat="server" and visible="false". Remove <% If x = 1 Then %>, <% Else %>
and <% End If %>
In the class of the usercontrol add a method to switch the visibilities,
i.e.:

Public Sub SwitchVisibility(ByVal showFirstPanel As Boolean)
panel1.Visible = showFirstPanel
panel2.Visible = Not(showFirstPanel)
End Sub

Call this method on the instance of the usercontrol from the Page_Load of
the Page with the parameter you need.

Greetings
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top