Asp.net 以编程方式将图像放置在范围内
将图像以编程方式放置在Span中(此处:Asp.net 以编程方式将图像放置在范围内,asp.net,html,Asp.net,Html,将图像以编程方式放置在Span中(此处:HtmlGenericControl) 希望以后通过FindControl HtmlGenericControl spnMeta = new HtmlGenericControl("span") { ID = "spanMeta" }; //Literal litMeta = new Literal(); //litEdit.Text = "<span id=\"spanMet
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
图像将被渲染到跨度之外及其旁边
以前,我通过Literal创建了这个过程,因为它在下面的代码块中被注释掉了
但那个时候我似乎无法从服务器代码访问它
我如何才能做到这一点?以下是一些示例代码,用于通过代码将图像放置到占位符中: ASPX
<div>
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
</div>
这是输出:
<div>
<span id="spanMeta"><img id="imgCodeIcon" src="../Images/Code-icon.gif" style="height:11px;width:15px;" /></span>
</div>
以下是一些通过代码将图像放置到占位符中的示例代码: ASPX
<div>
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
</div>
这是输出:
<div>
<span id="spanMeta"><img id="imgCodeIcon" src="../Images/Code-icon.gif" style="height:11px;width:15px;" /></span>
</div>
这样做是不可能的:spnMeta.Controls.Add(imgCodeIcon);怎么做?你会出错吗?我做了,它工作正常。好的,我会玩它,也许问题在于另一个部分或遗漏的部分,我会探究它,谢谢。这是不可能的:spnMeta.Controls.Add(imgCodeIcon);怎么做?你会出错吗?我做了,它工作正常。好的,我会玩它,也许问题是另一个部分或遗漏的部分,我会探究它,谢谢你确定你提供的输出吗?跨距是否覆盖了图像?唯一的区别是我有一个占位符控件,我正在将跨距标记添加到该控件。我看到了,它能完成任务吗?我还没有测试,不管怎样,谢谢参与。没问题。是的,它可以工作,请参阅我上面发布的输出。如果不使用占位符,它将添加到HTML页面标记的最末尾。如果您使用占位符控件,那么它将被添加到您想要的位置。所以使用占位符。你确定你提供的输出吗?跨距是否覆盖了图像?唯一的区别是我有一个占位符控件,我正在将跨距标记添加到该控件。我看到了,它能完成任务吗?我还没有测试,不管怎样,谢谢参与。没问题。是的,它可以工作,请参阅我上面发布的输出。如果不使用占位符,它将添加到HTML页面标记的最末尾。如果您使用占位符控件,那么它将被添加到您想要的位置。所以使用占位符。