.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to add controls in the asp:Panel and how to control its output.

Posted By:Mukul       Posted Date: May 24, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article, I am going to explain how to add controls in the asp:Panel and how to control its output.
 

Introduction:

Normally the Panel control is used as a container and works as a wrapper for other html or asp.net controls.  This control is often used to generate controls dynamically and add it to the asp:Panel. It can also be used to display and hide group of controls.

To read hundreds of .NET How to's like this, click here.

In this example, we will use an asp:Panel control and some text to display as an output. This is actually not very large, but it should serve the purpose of showing what can be done with the asp:Panel.

So to use this approach, add below code in your ASPX page.

ASPX Page:

        This content is inside the Panel control.
CODE Behind:

protected void Page_Load(object sender, EventArgs e)
{
    Label lbl = new Label();
    lbl.Text = "This is the text of Literal control";
    lbl.ID = "litLiteral";
    Panel1.Controls.Add(new LiteralControl("

This is added programmatically

")); Panel1.Controls.Add(lbl); }

In the above code, we have an asp:Panel control in which we are adding a static text as well as dynamically generated asp:Label control. To control its output, we can set Visible property to true or false based on our need (Visible property is available for all asp.net server control or html server control).

Now when we run the page with above code snippets, its output should appear something like below image.

Here the 1st text is static text written on the .aspx page. The 2nd text is written by adding the asp:Label control from the server side and the third text is also written by adding the LiteralControl from the server side.

Conclusion:

I hope you can see how easy it is to utilize asp:Panel controls in order to dynamically add/remove controls or hide/display controls from it.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend