Reputation: 1229
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.
Upvotes: 1
Views: 3409
Reputation: 17931
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]
Upvotes: 1
Reputation: 5423
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);
Upvotes: 1