c# - 添加 <asp :image> from codebehind

标签 c# asp.net webforms

我想添加 <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/

相关文章:

c# - 在异步方法中显示进度对话框

c# - 选择 4 个具有相同 CSS 类的按钮

.net - ASP.NET - 如何从用户控件中使用 Response.Redirect()? (.ascx)

asp.net - UpdatePanel 无法设置样式

c# - 在代码后面的 gridview 事件处理程序中切换 DIV 可见性不起作用

html - 无法将图像添加到登录页面

c# - 如何设置 Unity 注册约定?

c# - Json.Net JObject 的 Automapper 反射错误

asp.net - HttpContext.User 何时初始化?

php - 为自然语言形式开发配套 php 的问题