开发者

ASP.NET Image Control inside Panel

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜