以编程方式在范围(此处为HtmlGenericControl
)中放置图片
希望稍后通过FindControl
HtmlGenericControl spnMeta = new HtmlGenericControl("span")
{ ID = "spanMeta" };
//Literal litMeta = new Literal();
//litEdit.Text = "<span id=\"spanMeta\" class=\"meta\" runat=\"server\">" ;
Image imgCodeIcon = new Image()
{
ID="imgCodeIcon" ,
ImageUrl="../Images/Code-icon.gif" ,
Width = 15 , Height = 11
};
spnMeta.Controls.Add(imgCodeIcon);
// Shouldn't be important here ...
spnMeta.DataBinding +=new EventHandler(spnMeta_DataBinding);
// closing Span if Literal is used
这样做无效:
spnMeta.Controls.Add(imgCodeIcon);
图像将在跨度之外和旁边呈现
以前我通过Literal制作了程序,因为它在下面的代码块中被注释掉了 但那个时候似乎我无法从服务器代码访问它
我怎么能实现这个目标?
答案 0 :(得分:3)
以下是一些示例代码,用于通过代码将图像放入占位符:
<强> ASPX 强>
<div>
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
</div>
<强> C#强>
HtmlGenericControl spnMeta = new HtmlGenericControl("span") { ID = "spanMeta" };
Image imgCodeIcon = new Image()
{
ID = "imgCodeIcon",
ImageUrl = "../Images/Code-icon.gif",
Width = 15,
Height = 11
};
spnMeta.Controls.Add(imgCodeIcon);
ph1.Controls.Add(spnMeta);
这是输出:
<div>
<span id="spanMeta"><img id="imgCodeIcon" src="../Images/Code-icon.gif" style="height:11px;width:15px;" /></span>
</div>