[karsten samaschke]

ASP.NET daily. Or weekly.

How to change the text in a panel

A tricky question is this one:

How can I change the text in an asp:Panel?

Let's assume, a panel like this is given in your ASPX-page:

<asp:Panel ID="thePanel" runat="server">Hi, this is some text</asp:Panel>

Usually, you would start searching for a .Text- or .InnerText-Property. But - and this is the sad news - no such property exists. You don't have the chance to change the panel's text.

Really?

No, of course you can change the text. See the inner text as a LiteralControl and it will work. In your CodeBehind, you'll have to write code like this:

[VB.NET]
Dim objPanelText As LiteralControl = _
   CType(thePanel.Controls.Item(0), LiteralControl)
objPanelText.Text = "Welcome to my VB.NET-page!"

[C#]
LiteralControl objPanelText = thePanel.Controls[0] as LiteralControl;
objPanelText.Text = "Welcome to my C#-page!";

That's it. Really simple, isn't it?

Comments

nerd said:

yes it is ;-)
# November 27, 2003 10:01 AM

Suresh said:

Dear Yes it is working... But it is not giving me complete solution... I want to write run time table with formate text & formated out on run time..on that time it not working
# August 6, 2004 1:37 AM

Thomas said:

No, this.panel1.text or this.panel1.inertext, that will have been realy simple.

# April 2, 2008 3:42 AM

joe said:

thank you so much

this is just what i was looking for

the only problem i was having with it was a line break not shoing up whichwas solved by:(vb)

objPanelText.Text = string.Replace(vbCrLf, "<br />")

# April 30, 2008 11:35 AM

damato said:

Good man worked perfect!!

# May 12, 2008 11:50 AM

asipo said:

Nice tutorial, I have been looking for this.

Going to put this link as reference to other site

# April 18, 2009 2:59 PM

NitinSawant said:

gr8

# August 26, 2009 9:45 AM

DBConner said:

You... youre good. And funny too. Like that.

# February 11, 2010 1:15 AM

SONA said:

THIS REALLY HELPED ME.. THNKYA

# April 22, 2010 7:20 AM

Andrew said:

Fantastic! accepts html too

# August 11, 2010 2:33 PM

Babu said:

Panel contentOutput = new Panel();

LiteralControl objPanelText = contentOutput.Controls[0] as LiteralControl;

LiteralControl objPanelText = contentOutput as LiteralControl;

None of it works

# January 22, 2011 4:12 AM

Dave said:

Exactly what I needed, unfortunatly it doesnt work for me :( get a nullpointer exception on the controls..

# March 21, 2011 7:12 AM

bet32978 said:

great.

# December 22, 2011 9:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)