473,785 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "<%=myImageFile Name%>", 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 = "<%=myImageFile Name
%>" />

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 = "<%=myImageFile Name
%>" />
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="<%=myImage FileName %>" 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 1727
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.n etwrote in message
news:0a******** *************** ***********@d77 g2000hsb.google groups.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 "<%=myImageFile Name%>", 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 = "<%=myImageFile Name
%>" />

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 = "<%=myImageFile Name
%>" />
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="<%=myImage FileName %>" 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.n etwrote in message
news:f1******** *************** ***********@h17 g2000prg.google groups.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.P age
{
public string myImageFileName = "something" ;

protected void Page_Load(objec t sender, EventArgs e)
{

}
}

You then have the following in your page:

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

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.P age
{
public string myImageFileName = "something" ;

protected void Page_Load(objec t 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.P age
{
public string myImageFileName = "something" ;

public string GetImageString( )
{
return myImageFileName ;
}

protected void Page_Load(objec t 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.j pg" />

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.P age
{
#region Declarations

public string myImageFileName = "something" ;

#endregion //Declarations

#region Events

protected void Page_Load(objec t 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.ImageUr l = 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.n etwrote in message
news:f1******** *************** ***********@h17 g2000prg.google groups.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.n etwrote in message
news:f8******** *************** ***********@r15 g2000prd.google groups.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
13822
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, and display that number. So, that page would look something like: Number of calls today: 5 Add | Reset The "5" would increment with every click of the "Add" link. The "Reset" link would reset the counter to 0.
1
7356
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 javascript can reference. Any idea how. Thanks
6
8213
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 gstrDataServer - holds the server root where a related data warehouse is stored The values of these variables are set when the database is opened. I want to retrieve one or more of these values in a query, but Access
1
7061
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 literal html in the aspx page. How to do for retrieving the value in my textarea without define a variable (runnat server) in c# code? I thought this code retrieve all controls and value in all tag inside the Form tag, it is not the case.
2
3968
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 has changed. How can I retrieve a control's previous value from the ViewState? I know that I could save the control's previous value in a session variable, reretrieve it's value from the db, etc. But it would appear the control's previous value...
2
2852
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 anyone suggest a btter way of passing a session var into a vbscript function? <%@ Page language="c#" debug="true" ContentType="text/html"
2
3623
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 Browser URL box, %USERNAME% is repalced with the local machine ID. However, when I create a <a href="http://RemoteServerName/testusername.asp?id=%USERNAME%"> get mahcine id</a> in an html file and use Request.Querystring("id") to retrieve it in...
2
3732
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 & UserID are two of the columns in the DB table). So for e.g. consider a user whose UserID=6 & the DB table has 3 records whose UserID=6. In other words, there are 3 OrderID records of the user whose UserID=6, say, OrderID=8, OrderID=17 & OrderID=29....
2
245
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 runat="server" ID="myImage" ImageUrl = "<%=myImageFileName %>" /> The variable myImageFileName is set in either the Page_Init or Page_Load
0
10324
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...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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,...
1
7499
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
6739
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
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
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.