【问题标题】:How to use asp:image in code behind如何在后面的代码中使用 asp:image
【发布时间】:2014-08-05 08:27:39
【问题描述】:

在后面的代码中渲染 asp:image 时遇到问题。 首先我解释一下我的方法: 在Default.aspx 中,我只使用一个标签。 在后面的代码中,我创建了一个字符串变量并填充它,然后用我的字符串变量填充lable.Text
这是我的Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
   <div>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   </div>
</form>
</body>
</html>

这是我在 Defaul.aspx.cs 中的表单加载函数:

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = @"<asp:Image ID='Image1' runat='server'   ImageUrl='~/images/sc/tiraje.jpg' />
                    <br/>
                    <img src='../images/sc/tiraje.jpg' />
                    ";
}

现在这段代码必须渲染两个图像。但是 asp:image 使用的第一张图片不起作用!我需要在我的字符串变量中使用 asp:image 因为我的 URL 会改变我的结果,例如如果我的 URL 是:

http://localhost:19551/website/test/Default.aspx

当我给它一个“/”时:

http://localhost:19551/website/test/Default.aspx/ 

第二个 URL 导致更改我在 &lt;img&gt; 标记中使用的字符串中第二个图像的 src。所以我想使用 asp:image 因为它使用“~/”作为 src (imageUrl) 永远不会因更改 URL 而改变!

【问题讨论】:

  • 使用 asp:image 控件
  • 为什么要使用标签?如果要使用服务器端图像控件,为什么不使用占位符控件?对于放置 html 图像标签,您可以使用不会将图像标签包装在跨度标签中的文字。
  • 感谢您的帮助,您说得对,但我需要在占位符中添加一些 html 标签,如何在占位符中添加 asp:image 和简单的 html 标签,例如可以你在 2 asp:image 之后添加 1 div,然后在占位符内添加 1

标签: c# html asp.net image code-behind


【解决方案1】:

Abdullah ELEN 引入的 Literal 控件是最简单且有效的。像这样:

       <asp:Literal ID="Literal1" runat="server"></asp:Literal>

然后在后面的代码中,假设你已经将图像源捕获到一个局部变量(photo_src)中,你添加这个:

        Literal1.Text += "<img src=" + '"' + photo_src + '"' + "/>";

【讨论】:

    【解决方案2】:

    您不能以这种方式创建,因为Asp:Image 是一个服务器对象。下面我为大家准备了一个简单的例子。

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="pageDefault" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
       <div>
          <asp:Literal ID="literalControl" runat="server" />
       </div>
    </form>
    </body>
    </html>
    

    代码隐藏

    protected void Page_Load(object sender, EventArgs e)
    {
        // Write here your SQL query for image URLs..
    
        while(reader.Read())
            {
                literalControl.Text +=
                    "<img src=\"../images/sc/" + reader[0].ToString() + ".jpg\" /><br/>";
            }
    }
    

    【讨论】:

      【解决方案3】:

      您不应该使用标签来呈现图像。而是在 web 表单中放置两个 asp:image 控件或使用占位符控件,例如:

      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Application.WebForm1" %>
      
      <!DOCTYPE html>
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
      </head>
      <body>
          <form id="form1" runat="server">
          <div>
              <asp:Image runat="server" ID="Image1" />
              <br />
              <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
          </div>
          </form>
      </body>
      </html>
      

      然后你可以动态创建一个新的图像控件,并使用它们的 ImageUrl 属性通过代码隐藏设置图像 url:

      public partial class WebForm1 : System.Web.UI.Page
          {
              protected void Page_Load(object sender, EventArgs e)
              {
                  Image1.ImageUrl = "../images/sc/tiraje.jpg";
      
                  Image Image2 = new Image();
                  Image2.ImageUrl = "../images/sc/tiraje.jpg";
      
                  PlaceHolder1.Controls.Add(Image2);
              }
          }
      

      【讨论】:

      • Orilux 感谢您的回答,但我应该使用 lable 执行此操作,因为我有许多来自数据库和其他任何东西的动态图像,因此我需要通过生成 html 在代码隐藏中执行此操作,然后将其填充到我的html页面中的渲染标签
      • 我添加的占位符控件在那里而不是标签。查看我在代码隐藏中创建 Image2 并将其添加到占位符的方式。
      • 我必须在代码后面生成代码。我没有任何办法帮助我解决如何在代码后面使用 asp:image 代替 标记的问题?或者如何定义我的 url 不影响我的 src 属性!
      猜你喜欢
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多