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" <donliu@telus.netwrote in message
news:f1dbd584-62a6-4249-9c2f-f8fa9425e546@h17g2000prg.googlegroups.com...
Quote:
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.