tmutton
tmutton

Reputation: 1101

Problem adding control using AddAt method [ASP.NET]

On my page I have the following Image control.

    <asp:Image ID="Image1" ImageUrl="~/images/icons/returnarrow.gif" runat="server" />

Then on Page_Load I am doing the following.

    Image1.Controls.AddAt(1, new LiteralControl("ChildControl2"));

So what this should do (theoretically) is add a new LiteralControl next to the image. But it doesn't. However, If I change the index to 0 and the Image to a GridView control it works.

What am I doing wrong?

Upvotes: 0

Views: 1223

Answers (3)

James Johnson
James Johnson

Reputation: 46047

You should be trying to embed another control in an image. Use a PlaceHolder as the parent container, and add the Image and Literal to the PlaceHolder.

Upvotes: 1

Waqas
Waqas

Reputation: 6802

I don't think so Image1.Controls.AddAt will add any control next to Image control because Image1.Controls is a collection of child controls of Image1, may be this.Controls.AddAt will work

Upvotes: 1

onof
onof

Reputation: 17367

Don't modify the Controls collection. Use a PlaceHolder instead.

Upvotes: 2

Related Questions