简体   繁体   中英

How can i add new panel at run time in asp.net

I am working on an asp.net web application where I have predefined panel in my project with CSS. Now i want to create another panel with same design and CSS at run time at multiple times. I have a button control when i will click that button it will add another panel.

Please help me to add another panel with same criteria.

If it is something that you plan on reusing, I'd suggest you utilize a user control for this. You can them simply add a new instance of the control on your page.

A few things worth looking into:

  1. http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
  2. http://aspalliance.com/565
  3. http://msdn.microsoft.com/en-us/library/c0az2h86.aspx

If you wanted to accomplish this with a postback to the page, add this to your event...

//MyControl = Custom User Control
var myControl =  (MyControl) Page.LoadControl("MyControl.ascx"); 
this.ControlContainer.Controls.Add(myControl);

Like RSolberg said, you could write a User Control and add it multiple times:

<my:UserControl id="MyControl1" runat="server" />
<my:UserControl id="MyControl2" runat="server" />
<my:UserControl id="MyControl3" runat="server" />

Of course, your User Control can be as simple or as complex as you like, thus having repeated functionality on your page.

However, depending on your exact needs you might want to consider something like an ASP.NET Repeater, or ListView, or DataGrid control. With something like a Repeater, you can bind data to it, and have that data be displayed in a list/grid, that has a common look and feel. You can give your Repeater a HTML/CSS template for the header, items, and footer sections too to make it look consistent and professional.

<asp:Repeater id="MyRepeater" runat="server">
  <HeaderTemplate>
    <h1>Products</h1>
  </HeaderTemplate>
  <ItemTemplate>
    <p>
      Product name: <%# Eval("ProductName") %>
    </p>
  </ItemTemplate>
</asp:Repeater>

and in your code just do this:

MyRepeater.DataSource = products;
MyRepeater.DataBind();

There are many ways of doing what you're asking in ASP.NET - be a bit more specific and we'll be able to give you more specifc help.

Couldn't you just create a new panel in the code behind on the button's "On_Click" event? That would be my suggestion. You may need to have a placeholder to add the panel into something so it appears on the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM