Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set an image source dynamically from a variable or QueryString 1

Status
Not open for further replies.

Quickfix99

Technical User
May 16, 2007
27
CA
Hi,

I have a simple request. How do I set the source of an image in an ASP page using a variable? Example:

<script runat="server">
protected void Page_Load(Object Source, EventArgs E)
{
String Image = "}
</script>

<asp:Image ID="Image1" runat="server" ImageUrl = (Image Variable) />

Any reply would be greatly appreciated!

QF
 
I tried that but it appears I am using the code in the wrong area or something?

CS0103: The name 'IMAGE1' does not exist in the current context

 
Show the code that causes that error.. and the HTML of the page as well.
 
<script runat="server">
protected void Page_Load(Object Source, EventArgs E)
{
String IMAGE = Request.QueryString["IMAGE"];
Response.Write(Request.QueryString["IMAGE"]);
Image1.ImageSrc = IMAGE;
}
</script>
</head>
<body>

<div style="width: 640px; height:480 px;">
<img id = "Image1" runat ="server" />
</div>

</body>
</html>
 
I don't see a <form> tag in your HTML.. I belive that is the reason why the control is no being recognized.
 
Oops, I missed it in my copy and paste operation...The form tag is there.

thx


<script runat="server">
protected void Page_Load(Object Source, EventArgs E)
{
String IMAGE = Request.QueryString["IMAGE"];
Response.Write(Request.QueryString["IMAGE"]);
Image1.ImageSrc = IMAGE;
}
</script>
</head>
<body>
<script runat="server">
protected void Page_Load(Object Source, EventArgs E)
{
String IMAGE = Request.QueryString["IMAGE"];
Response.Write(Request.QueryString["IMAGE"]);
Image1.ImageSrc = IMAGE;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 640px; height:480 px;">
<img id = "Image1" runat ="server" />
</div>

</form>
</body>
</html>
<div style="width: 640px; height:480 px;">
<img id = "Image1" runat ="server" />
</div>

</body>
</html>
 
Code:
<img id = "Image1" runat ="server" />
Shouldn't this be:
Code:
<asp:image runat="server" id="Image1" />

I'm using the code behind approach right now and it works perfectly!
Brett
 
I still get an error though:

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.Image' does not contain a definition for 'ImageURL'

Source Error:

Line 16: Image1.ImageURL = IMAGE;
Line 17:
Line 18:

 
Yes, VS 2005. I have moved the Page Load code to the code behind class. I no longer get the no definition error. In fact I don't get any errors other than the image not displaying. Does the ImageURL string have to be in any special format?

Thanks
 
OK, I figured it out....I needed to remove the Page_Load method from my asp page (even though there was no code in there).

Thanks very much for your help. I learn a lot this way!

Mark
 
No problem, just remember not to use inline coding as you were.. Keep all coding in your code behind file or class files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top