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

  • Thread starter Thread starter data
  • Start date Start date
D

data

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.
 
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.
 
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! |
********************************************
 
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!
 
Back
Top