如何在后面的代码中使用asp:image
我在后台代码中渲染asp:image时遇到问题。
首先我解释一下我的方法:
在 Default.aspx
我只使用一个标签。
在后面的代码中,我创建一个字符串变量并填充它,然后通过字符串变量填充 lable.Text
。
这是我的 Default.aspx
:
I have a problem with rendering asp:image in code behind.
First I explain my method:
In Default.aspx
I just use one label.
In code behind I make a string variable and fill it, then I fill the lable.Text
by my string variable.
this is my 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中的表单加载函数:
and this is my form load function in 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是:
Now this code must render two images. But first image used by asp:image doesn't work! I need to use asp:image in my string variable because my URL changes my result, for example if my URL is:
http://localhost:19551/website/test/Default.aspx
当我给它一个/ :
http://localhost:19551/website/test/Default.aspx/
第二个URL会导致更改我在< img>中使用的字符串中第二个图像的src
标签。因此,我想使用asp:image,因为它使用〜/表示永远不会被更改URL更改的src(imageUrl)!
The second URL causes to change the src of second image in my string that I use in <img>
tag. so that I want to use asp:image because it uses "~/" for src (imageUrl) that never changes by change URL!
阿卜杜拉·艾伦(Abdullah ELEN)介绍的文字控制是最简单的,也是有效的。像这样:
The Literal control introduced by Abdullah ELEN is the easiest and it works. Like this:
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
然后在后面的代码中,假设您已经将图像源捕获到本地变量中(photo_src ),你添加这个:
And then in the code behind, assuming you have already captured the image source into a local variable (photo_src), you add this:
Literal1.Text += "<img src=" + '"' + photo_src + '"' + "/>";