473,698 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Scope & Page.DataBind() ?

The following code works fine:

<script runat="server">
Dim strName As String = "Arpan"
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>
</form>

But if the line

Dim strName As String = "Arpan"

is placed inside the Page_Load sub so that the script code looks like
this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim strName As String = "Arpan"
Page.DataBind()
End Sub
</script>

then the following error gets generated:

Name 'strName' is not declared.

pointing to the line

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

Why so?

Thanks,

Arpan

Aug 1 '06 #1
4 1821
Why not just have a line like

lblMessage.Text = strName

In your Page_Load event handler.

"Arpan" wrote:
The following code works fine:

<script runat="server">
Dim strName As String = "Arpan"
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>
</form>

But if the line

Dim strName As String = "Arpan"

is placed inside the Page_Load sub so that the script code looks like
this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim strName As String = "Arpan"
Page.DataBind()
End Sub
</script>

then the following error gets generated:

Name 'strName' is not declared.

pointing to the line

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

Why so?

Thanks,

Arpan

Aug 1 '06 #2
clickon, I very well am aware of the fact that it can be done in the
way you have shown which, I believe, is the easiest way out but being a
newbie as far as DataBinding is concerned (which I thought goes without
saying since had I not known that simple 1-line code that you have
shown which any ASP.NET newbie learns at the very beginning, I don't
think I, or for that matter, any newbie, would have posted this basic
question on DataBinding), I did like to grasp the fundamentals of
DataBinding.

Moreover I am not asking for any alternative code; rather I did like to
know why the error.gets generated when the variable "strName" is
declared & assigned a value in the Page_Load sub.

Arpan

clickon wrote:
Why not just have a line like

lblMessage.Text = strName

In your Page_Load event handler.

"Arpan" wrote:
The following code works fine:

<script runat="server">
Dim strName As String = "Arpan"
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>
</form>

But if the line

Dim strName As String = "Arpan"

is placed inside the Page_Load sub so that the script code looks like
this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim strName As String = "Arpan"
Page.DataBind()
End Sub
</script>

then the following error gets generated:

Name 'strName' is not declared.

pointing to the line

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

Why so?

Thanks,

Arpan
Aug 1 '06 #3
Arpan,

when you declare a variable inside a page load event handler, its not a
member of page class, so when asp.net generates c# code for your there
is no variable named strName, and hence is complains.

Following line of code would look for a protected (private if not using
code behind model) property/variable named strName in your page class

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

hth
Arpan wrote:
clickon, I very well am aware of the fact that it can be done in the
way you have shown which, I believe, is the easiest way out but being a
newbie as far as DataBinding is concerned (which I thought goes without
saying since had I not known that simple 1-line code that you have
shown which any ASP.NET newbie learns at the very beginning, I don't
think I, or for that matter, any newbie, would have posted this basic
question on DataBinding), I did like to grasp the fundamentals of
DataBinding.

Moreover I am not asking for any alternative code; rather I did like to
know why the error.gets generated when the variable "strName" is
declared & assigned a value in the Page_Load sub.

Arpan

clickon wrote:
>Why not just have a line like

lblMessage.Tex t = strName

In your Page_Load event handler.

"Arpan" wrote:
>>The following code works fine:

<script runat="server">
Dim strName As String = "Arpan"
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>
</form>

But if the line

Dim strName As String = "Arpan"

is placed inside the Page_Load sub so that the script code looks like
this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim strName As String = "Arpan"
Page.DataBind()
End Sub
</script>

then the following error gets generated:

Name 'strName' is not declared.

pointing to the line

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

Why so?

Thanks,

Arpan

Aug 1 '06 #4
Thanks, Ashish, for your response.

What about variables declared within subs (be it user-defined or
built-in) other than the Page_Load sub - aren't they members of the
Page class as well? If yes, does that mean variables declared within
the <script runat="server"t ag but outside any sub automatically
become members of the Page class?

Thanks,

Regards,

Arpan
ashish wrote:
Arpan,

when you declare a variable inside a page load event handler, its not a
member of page class, so when asp.net generates c# code for your there
is no variable named strName, and hence is complains.

Following line of code would look for a protected (private if not using
code behind model) property/variable named strName in your page class

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

hth
Arpan wrote:
clickon, I very well am aware of the fact that it can be done in the
way you have shown which, I believe, is the easiest way out but being a
newbie as far as DataBinding is concerned (which I thought goes without
saying since had I not known that simple 1-line code that you have
shown which any ASP.NET newbie learns at the very beginning, I don't
think I, or for that matter, any newbie, would have posted this basic
question on DataBinding), I did like to grasp the fundamentals of
DataBinding.

Moreover I am not asking for any alternative code; rather I did like to
know why the error.gets generated when the variable "strName" is
declared & assigned a value in the Page_Load sub.

Arpan

clickon wrote:
Why not just have a line like

lblMessage.Text = strName

In your Page_Load event handler.

"Arpan" wrote:

The following code works fine:

<script runat="server">
Dim strName As String = "Arpan"
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>
</form>

But if the line

Dim strName As String = "Arpan"

is placed inside the Page_Load sub so that the script code looks like
this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim strName As String = "Arpan"
Page.DataBind()
End Sub
</script>

then the following error gets generated:

Name 'strName' is not declared.

pointing to the line

<asp:Label id="lblMessage " Text=<%# strName %runat="server"/>

Why so?

Thanks,

Arpan

Aug 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1951
by: Andrew Pasetti | last post by:
I'm having trouble binding/displaying a class variable to the display aspx page. Perhaps someone can suggest what I am doing wrong. ################################### Within the .aspx page I have this: ################################### <td><%# p.ParcelName %></td> #############################################
8
4830
by: Jade | last post by:
I saw some web page saying I could do it this way in javascript: var iNumber = <%#publicvarname publicpropertyname %> but it doesn't seem to work. I have this piece of code here in javascript: <script language="javascript"> var sessionServer = "<%#jsTestServer%>"; alert(sessionServer); </script>
1
3677
by: Rob Wire | last post by:
Please let me know the preferred way to do the following. Accept a Variable in the calling page's URL of an ID. Set that variable to be global scope of the class so that it can be used throughout the functions from Page Load. Would like to send the ID variable as a SQL parameter to stored procedures for Select, Update, and Insert. What I have done so far to get around it is declare it in the SQL Select statements of the Web form code,...
2
3382
by: John Holmes | last post by:
I have a web interface where the user types in ID's one at a time. After an ID is typed in, a button is clicked and the button click event has code that does a query and returns a data reader and then appends the data to a dataset that is built in the Page_Load code in the if(!isPostBack) block. When I try to add a row in the button click event code I get an error saying that "Object reference not set to an instance of an object". I'm...
1
6422
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and binding it to a repeater control. This repeater control has multiple text boxes and buttons. Can you please tell me how can i do paging in this case ? I'm posting my code below. The problem is that if i click on "AdjustThisAd" button, it opens...
11
16948
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got the following frame structure at the top level: FRAMESET CODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html><head><title>Server List</title></head> <frameset frameborder="1" border="1" framespacing="1" rows="10%,89%">
1
25682
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
1
1561
by: mister-Ed | last post by:
I'm trying to check for "No Records Found" in a repeater list control, but my 'numrows' variable cannot be seen by the function CheckNoR , even tho it's public (?) . I've been informed that the page class has to have these declared as member variables, but still cannot see why this won't work: <script runat="server"> 'Handle page load event public Sub Page_Load(Sender As Object, E As EventArgs)
5
6133
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request scope. Application variables are then accessed in this manner Request.App.<Var>. To begin with I had a simple functioning login system inside a subdirectory named admin, this subdirectory had it's own application.cfm, I wasn't sure whether to duplicate...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.