I've been looking about and cant figure out how to get a value from C# into the tag on an ASPX page. I've tried a number of options but not getting anywhere.
When my asp page loads, a value is obtained using queryString from the pages url (ie mypage.aspx?app=safety ) It then runs a switch to find out what image url to use on the page.
Thing is, I get a Compilation Error "The name 'img_small' does not exist in the current context". What do you think of my code below? I cant see what Im missing!
C#:
protected void Page_Load(object sender, EventArgs e)
{
string img_small;
String appName = Request.QueryString["app"];
switch (appName)
{
case "safety":
img_small = "safety-logo.png";
break;
case "files":
img_small = "files-logo.png";
break;
case "drawings":
img_small = "drawings-logo.png";
break;
case "specs":
img_small = "specs-logo.png";
break;
default:
img_small = "idms-logo.png";
break;
}
}
HTML:
<img src='"<%=img_small%>"' />
img_small
doesn't exist outside of Page_Load
.
You need to use at a minimum an internal field with that name in order to be able to access it anywhere in the class, including the .aspx
.
public string img_small;
protected void Page_Load(object sender, EventArgs e)
{
....
This is because img_small
is a local variable of Page_Load
function, it doesn't exist outside this function. This is why you get this error.
I'd suggested you to change
<img src='"<%=img_small%>"' />
to
<asp:Image ID='imgImage' runat='server' />
And then at the bottom of Page_Load
function do
imgImage.ImageUrl = img_small;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.