views:

32

answers:

2

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

Panel1.Controls.Add(new Image { ImageUrl = 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.

A: 

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);
Dan Dumitru
Yes you were right. There were space characters between <panel></panel> and guess that was the literal control i was getting.
smkngspcmn
A: 

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]

ajay_whiz