473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do I retrieve a variable in an ASP.Net server control?

I searched the internet and saw an old posting which has the same
problem I am experiencing. The asp server doesn't recognize the
variable I declare publicly in my codebehind class. The variable
works fine if I just display in the page "<%=myImageFileName%>", but
the server control doesn't recognize it. Is it a miscrosoft bug or
did I got the syntax wrong? I am stuck. Please help me. My imageurl
is dynamicly generated during page load. So I can't just hard-code it
in the page.
Thanks!

<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
%>" />

This is the original posting and I got the exact same problem.
================================================== ========

Pardon. This question seems incredibly dumb but I seem to
be suffering a brain block. I want an ASP.Net 2.0 image control
to contain a variable for the image file name as shown...

<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
%>" />
The variable myImageFileName is set in either the Page_Init or
Page_Load
event handler. The markup appears on a master page which is used
used by .aspx pages in different folders, and at different levels of
indirection with respect to the project root.
Is it that the script is being rendered before Page_Init?
Doesn't sound right. In fact, if I write
<td><% = myImageFileName %></td>
the file name shows up on the rendered page as expected.
I believe the previous image tag works if the feature is a client-
side
HTML tag. That is,
<img src="<%=myImageFileName %>" alt="" />
will *probably* produce the desired result. However, I have used an
active control because the path name in the file name contains the
"~" character, representing the project root which of course
is meaningless in an HTML tag.
Jul 26 '08 #1
6 1705
Without seeing the code behind, I have no clue what you are trying to do.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"data" <do****@telus.netwrote in message
news:0a**********************************@d77g2000 hsb.googlegroups.com...
>I searched the internet and saw an old posting which has the same
problem I am experiencing. The asp server doesn't recognize the
variable I declare publicly in my codebehind class. The variable
works fine if I just display in the page "<%=myImageFileName%>", but
the server control doesn't recognize it. Is it a miscrosoft bug or
did I got the syntax wrong? I am stuck. Please help me. My imageurl
is dynamicly generated during page load. So I can't just hard-code it
in the page.
Thanks!

<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
%>" />

This is the original posting and I got the exact same problem.
================================================== ========

Pardon. This question seems incredibly dumb but I seem to
be suffering a brain block. I want an ASP.Net 2.0 image control
to contain a variable for the image file name as shown...

<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
%>" />
The variable myImageFileName is set in either the Page_Init or
Page_Load
event handler. The markup appears on a master page which is used
used by .aspx pages in different folders, and at different levels of
indirection with respect to the project root.
Is it that the script is being rendered before Page_Init?
Doesn't sound right. In fact, if I write
<td><% = myImageFileName %></td>
the file name shows up on the rendered page as expected.
I believe the previous image tag works if the feature is a client-
side
HTML tag. That is,
<img src="<%=myImageFileName %>" alt="" />
will *probably* produce the desired result. However, I have used an
active control because the path name in the file name contains the
"~" character, representing the project root which of course
is meaningless in an HTML tag.
Jul 26 '08 #2
In the c# class, I simple declare myImageFileName and update it with a
different value when a page load everyday. The problem is asp server
control can't use a member variable declared in the c# file. Is that a
limitation of asp.net2 or something i did wrong in the syntax.
Jul 26 '08 #3
Please show your code so we can help.
"data" <do****@telus.netwrote in message
news:f1**********************************@h17g2000 prg.googlegroups.com...
In the c# class, I simple declare myImageFileName and update it with a
different value when a page load everyday. The problem is asp server
control can't use a member variable declared in the c# file. Is that a
limitation of asp.net2 or something i did wrong in the syntax.

Jul 26 '08 #4
First, I do not like simple binding if you are already doing something in
code behind, but let's play with your example. You are probably missing two
things. I can see one straight up. Let's assume the following page:

using System;

public partial class _Default : System.Web.UI.Page
{
public string myImageFileName = "something";

protected void Page_Load(object sender, EventArgs e)
{

}
}

You then have the following in your page:

<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName%>" />

The first thing to change is the ImageUrl= part to look like this:

<asp:Image runat="server" ID="myImage" ImageUrl = "<%# myImageFileName %>"
/>

The pound here (#) tells us we are databinding. You then have to alter
Page_Load() to bind:

using System;

public partial class _Default : System.Web.UI.Page
{
public string myImageFileName = "something";

protected void Page_Load(object sender, EventArgs e)
{
//Add this
Page.DataBind();
}
}

The page now works. But, you really should use a method of some sort, as I
assume this is going to be dynamic. So the first refactor is something like
this:

using System;

public partial class _Default : System.Web.UI.Page
{
public string myImageFileName = "something";

public string GetImageString()
{
return myImageFileName;
}

protected void Page_Load(object sender, EventArgs e)
{
//Add this
Page.DataBind();
}
}

with the page

<asp:Image runat="server" ID="myImage" ImageUrl = "<%# GetImageString() %>"
/>

But, this is still pretty bad, as you have NO reason to simple bind when you
are already doing work in the code behind. So, change the tag to this:

<asp:Image runat="server" ID="myImage" ImageUrl = "defaultimage.jpg" />

And use the routine to pull in some binding method. I would say this is a
fairly decent refactor.

using System;

public partial class _Default : System.Web.UI.Page
{
#region Declarations

public string myImageFileName = "something";

#endregion //Declarations

#region Events

protected void Page_Load(object sender, EventArgs e)
{
//Call binding routine
BindImage();
}

#endregion //Events

#region Private Routines

private string GetImageString()
{
return myImageFileName;
}

private void BindImage()
{
//You will likely have more work here
myImage.ImageUrl = GetImageString();
}

#endregion //Private Routines
}

You can now alter the routine for binding or the routine that gets the
string. It is your choice.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"data" <do****@telus.netwrote in message
news:f1**********************************@h17g2000 prg.googlegroups.com...
In the c# class, I simple declare myImageFileName and update it with a
different value when a page load everyday. The problem is asp server
control can't use a member variable declared in the c# file. Is that a
limitation of asp.net2 or something i did wrong in the syntax.
Jul 26 '08 #5
Thank you so much, Gregory A. Beamer. I like your last suggestion the
best becauase I would like to code more in the codebehind class. I was
just so stuck wondering why I couldn't use the variable from c# in
aspx page's server control. Thanks again!
Jul 26 '08 #6
Binding is a complex topic. Once you understand the rules, it gets easy, but
it is difficult to start out.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"data" <do****@telus.netwrote in message
news:f8**********************************@r15g2000 prd.googlegroups.com...
Thank you so much, Gregory A. Beamer. I like your last suggestion the
best becauase I would like to code more in the codebehind class. I was
just so stuck wondering why I couldn't use the variable from c# in
aspx page's server control. Thanks again!
Jul 29 '08 #7

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

Similar topics

6
by: Geoff | last post by:
Here's the situation. I have a static html page which we want to update to include some dynamic content. I want a counter that keeps track of the number of times anyone presses the "add" button,...
1
by: Bryan | last post by:
Hello: I need to retrieve the value of a textbox control in a asp.net datagrid using Javascript. Can anybody help out. I believe the control clientID needs to be passed client side so the...
6
by: Salvani Langosta | last post by:
In an Access 97 database, I use serveral global variables that hold information about the database, for example: gstrFileServer - holds the server root where the database is stored...
1
by: zoltix | last post by:
Hi, How to retrieve a value in in an aspx page? Normally it is easy, drag and drops the textarea in aspx page and it is work. But in this case, I generate the html code manually () and put as...
2
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
2
by: Frank | last post by:
Can I do this? I add a session var in C# and ultimatly want to pass it into a vbscript client side activeX control. This is what I have so far but get " Object Required:'name2' " error. Can...
2
by: adam | last post by:
Hi ASP Expert, My goal is to retrieve my local machine's %USERNAME% environment variable from ASP page. When I enter http://RemoteServerName/testusername.asp?id=%USERNAME% directly into the IE...
2
by: rn5a | last post by:
A SQL Server 2005 stored procedure expects a parameter 'UserID' depending upon which it retrieves the no. of records & OrderIDs corresponding to the 'UserID' from a DB table (note that OrderID &...
2
by: JimCinLA | last post by:
Pardon. This question seems incredibly dumb but I seem to be suffering a brain block. I want an ASP.Net 2.0 image control to contain a variable for the image file name as shown... <asp:Image...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.