2013-01-08 89 views
1

我一直在寻找和不能找出如何从C#中获取值到ASPX页面上的标记。我尝试了一些选项,但没有得到任何地方。用C#字符串替换图像URL

当我的ASP页面加载时,使用来自页面url的queryString(即mypage.aspx?app = safety)获得一个值然后运行一个开关以找出在页面上使用的图像URL。

事情是,我得到一个编译错误“名称'img_small'在当前上下文中不存在”。你如何看待我的代码?我看不到我失踪了!

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%>"' /> 

回答

2

这是因为img_smallPage_Load函数的局部变量,它没有这个功能之外存在。这就是你得到这个错误的原因。

我会建议你改变

<img src='"<%=img_small%>"' /> 

<asp:Image ID='imgImage' runat='server' /> 

然后在Page_Load功能的底部做

imgImage.ImageUrl = img_small; 
5

img_small没有的Page_Load之外存在。

您至少需要使用带有该名称的内部字段才能在课程中的任何位置访问该字段,包括.aspx

public string img_small; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    ....