我想添加 <asp:image>
从我的代码隐藏到页面上。我网站上的用户将上传 X 张图片,我不想添加 <asp:images>
图片上传前。
我想添加 <asp:image>
的原因就是给用户展示上传的图片。
如何做到这一点?
我已经看到正在使用的占位符 Controller ,但是当变量必须不同时如何使用它呢?对于每次上传,都应将图像添加到网站上。
最佳答案
您可以通过创建一个新的 Image 对象变量,然后正确分配其属性,然后将图像附加到页面上的现有对象来实现。
Image myImg = new Image();
myImg.ImageUrl = "path_of_the_image.jpg";
myImg.Visible = true;
Panel1.Controls.Add(myImg); //You can attach the image to any control on your page
或者类似的:
this.Controls.Add(myImg); //Incase you wanted the image on your page controls
Label1.Controls.Add(myImg); //Incase you wanted the image to appear in a certain label
但是,此对象不会超出您执行的代码的生命周期。这意味着,您以后将无法与该对象进行交互,因为它是在 Page_Init 的范围之后定义的。如果您想动态添加此图像然后能够与之交互,您应该在 Page_Init 事件期间添加它。
关于c# - 添加 <asp :image> from codebehind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231423/