开发者

ASP.NET Image Control inside Panel

开发者 https://www.devze.com 2023-01-16 14:19 出处:网络
I have a panel control to which i add image controls from code behind. Panel1.Controls.Add(new Image { Imag开发者_如何学JAVAeUrl = String.Format(\"img/{0}.gif\", x) });

I have a panel control to which i add image controls from code behind.

Panel1.Controls.Add(new Image { Imag开发者_如何学JAVAeUrl = String.Format("img/{0}.gif", x) });

However when i try to access the new image control to modify certain attributes, i get a literal control instead.

ie:

Panel1.Controls[0].Width=new Unit(10, UnitType.Pixel);

results in... "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.Image'"

Why?? Please help.


You probably have more controls inside your Panel, besides the Image one.

Try iterating through them:

foreach (Control ctrl in Panel1.Controls)
    if (ctrl is Image)
        ctrl.Width = new Unit(10, UnitType.Pixel);


In ASP.NET there is always a literal control attached to any control. So if you find it by index you will get it in odd places like Panel1.Controls[1]

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号