简体   繁体   English

如何在asp.net中运行时添加新面板

[英]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. 我正在开发一个asp.net Web应用程序,我在我的项目中使用CSS预定义了面板。 Now i want to create another panel with same design and CSS at run time at multiple times. 现在我想在运行时多次创建具有相同设计和CSS的另一个面板。 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 http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
  2. http://aspalliance.com/565 http://aspalliance.com/565
  3. http://msdn.microsoft.com/en-us/library/c0az2h86.aspx 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: 像RSolberg说的那样,你可以编写一个用户控件并多次添加它:

<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. 但是,根据您的确切需要,您可能需要考虑类似ASP.NET Repeater,ListView或DataGrid控件。 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. 使用Repeater之类的东西,您可以将数据绑定到它,并将数据显示在列表/网格中,具有共同的外观。 You can give your Repeater a HTML/CSS template for the header, items, and footer sections too to make it look consistent and professional. 您也可以为Repeater提供标题,项目和页脚部分的HTML / CSS模板,以使其看起来一致且专业。

<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. 有很多方法可以在ASP.NET中执行您所要求的内容 - 更具体一点,我们将能够为您提供更具体的帮助。

Couldn't you just create a new panel in the code behind on the button's "On_Click" event? 难道你不能在按钮的“On_Click”事件后面的代码中创建一个新的面板吗? That would be my suggestion. 那是我的建议。 You may need to have a placeholder to add the panel into something so it appears on the page. 您可能需要一个占位符才能将面板添加到页面上显示的内容中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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