TVicky
TVicky

Reputation: 389

How to place a programmatically added literal control in the right location in the web page?

I am adding a literal control to my web page using the following lines of code:

LiteralControl myObject = new LiteralControl();

myObject.Text =
        @"<OBJECT ID='MediaPlayer' WIDTH='640' HEIGHT='480' CLASSID='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' STANDBY='Loading Windows Media Player components...' TYPE='application/x-vlc-plugin'>
        <PARAM NAME='FileName' VALUE='" + "file:///" + path + "'> ... </OBJECT>';";
        Page.Controls.Add(myObject);

but this is getting added at the bottom of the page while I want it in the middle,How can I specify the location where I need to place this control?

Upvotes: 1

Views: 1654

Answers (3)

TVicky
TVicky

Reputation: 389

Suppose if controlA is the control before which I want to place my literal control myObject then,

Control parent = controlA.Parent;

parent.Controls.AddAt(parent.Controls.IndexOf(controlA), myObject);

I found this on stackoverflow itself and it worked for me.

Upvotes: 0

Aristos
Aristos

Reputation: 66641

Beside @Flater answer that is better to follow if you only have some text to add, one way to specify the position of the added controls is to add a PlaceHolder control.

<asp:PlaceHolder runat="server" id="phPlaceOnMe" />

and on code behind you add your controls as:

phPlaceOnMe.Controls.Add(myObject);

Upvotes: 0

Flater
Flater

Reputation: 13763

From experience, I would place the Literal declaration on your page (so you can choose its exact location), then set the control's Text property via code. If you do not enter any text, the element will be empty and will not render anything on the page.

On your aspx page, in the place where you want it:

<asp:Literal ID="myLiteral" runat="server" />

In your code:

myLiteral.Text = "...";

If you don't want anything to be visible, just ignore it. The control won't hinder the rest of the application's flow.

Upvotes: 1

Related Questions