simple code-behind question

Feb 28, 2010 at 3:43 PM

Hi All: Sorry for the newbie-ness, but I'm customizing a theme and I'm getting hung up on a simple c# issue with the masterpage codebehind...I basically have a simple banner rotator that picks a random number from 1-14...but I can't figure out what the syntax needs to be ... I have an asp:image object on the masterpage, and on the code-behind I just need:

Random objRandom = new Random((int)DateTime.Now.Ticks % Int32.MaxValue);

imgHeader = objRandom.Next(1, 14).ToString;

where "imgHeader" is the id of the asp:image.

I'm getting "imgHeader doesn't exist in the current context"...what should the current context be?

Feb 28, 2010 at 8:13 PM
Edited Feb 28, 2010 at 8:20 PM

Your variable is not declared.  If it is the ID of the image, then make sure that it is spelled exactly the same.  Remember, this is also case-sensitive  Also, 'ToString' is a method.  Finally, imgHeader is an image type, and you are attempting to assign a string to it.  You should be addressing a property of the imgHeader, probably something like ImageUrl.

Also, make sure that your your image has the runat="server" attribute, so something like <asp:Image runat="server" id="imgHeader" />

string myurl = "http://mywebsite/getimage.aspx?id=" + objRandom.Next(1, 14).ToString();
imgHeader.ImageUrl = myurl;

EDIT: Made some fixes.

Feb 28, 2010 at 8:49 PM
fstanek wrote:

Your variable is not declared.  If it is the ID of the image, then make sure that it is spelled exactly the same.  Remember, this is also case-sensitive  Also, 'ToString' is a method.  Finally, imgHeader is an image type, and you are attempting to assign a string to it.  You should be addressing a property of the imgHeader, probably something like ImageUrl.

Also, make sure that your your image has the runat="server" attribute, so something like <asp:Image runat="server" id="imgHeader" />

string myurl = "http://mywebsite/getimage.aspx?id=" + objRandom.Next(1, 14).ToString();
imgHeader.ImageUrl = myurl;

EDIT: Made some fixes.

 Thanks for that...I have the image with runat=server on the site.master page:

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

asp:Image runat="server" ID="banImg" />

and I'm using the syntax as you've provided above on site.master.cs (in the Page_load), but I still get a blank white page.

 

 

 

 

 

 

 

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

public partial class StandardSite : System.Web.UI.MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {
		if (Page.User.Identity.IsAuthenticated)
		{
			aLogin.InnerText = Resources.labels.logoff;
			aLogin.HRef = BlogEngine.Core.Utils.RelativeWebRoot + "login.aspx?logoff";
		}
		else
		{
			aLogin.HRef = BlogEngine.Core.Utils.RelativeWebRoot + "login.aspx";
			aLogin.InnerText = Resources.labels.login;
		}

        //: get a random num to assign the banner img
        Random objRandom = new Random((int)DateTime.Now.Ticks % Int32.MaxValue);
        string myurl = "http://www.mywebsite.com/images/headers/" + objRandom.Next(1, 14).ToString() + ".jpg";
        banImg.imageURL = myurl;
  }


}
</font></font><font size="2" color="#0000ff"></font> In visual studio, "banImg" is highlighted and intellisense pops up "The name 'banImg' does not exist in the current context"

 

Feb 28, 2010 at 9:35 PM

I'm assuming that it is just a typo that you do not have a "<" in from of asp:Image. Apart from that, I'm not sure what could be affecting it.  I'm assuming that your master inherits from StandardSite, and that you're not using the image control in inside a template definition like a ContentTempate or ItemTemplate.

Mar 1, 2010 at 1:40 PM
Edited Mar 1, 2010 at 2:25 PM
fstanek wrote:

I'm assuming that it is just a typo that you do not have a "<" in from of asp:Image. Apart from that, I'm not sure what could be affecting it.  I'm assuming that your master inherits from StandardSite, and that you're not using the image control in inside a template definition like a ContentTempate or ItemTemplate.

Aaargh...in the end, it turned out to be a different typo...I had "imageURL" instead of "ImageURL", and intellisense didn't pick it up because it was already displaying the "current context" error.

FWIW, I had this copy of BE.NET running as a sub-application within a website on a development server, and when I opened the entire website in Visual Web Developer, I was getting the "current context" errors because BE was viewed within the context of the main website. When I opened just the BE sub-directory in VWD, the current context errors went away, and then I noticed the typo in the image ID...file under "I am an idiot" :(