动态添加控件之方案
在网上面发现了一个很好的解决方案。
using System.Web.UI.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml;
using System.Collections;
using System.Web;
using System;
namespace cn.co
{

[ToolboxData("<{0}:DynamicControlsPanel runat=server></{0}:DynamicControlsPanel>")]
public class DynamicControlsPanel : Panel
{
自定义的属性 自定义的属性
视图状态管理 视图状态管理
重化控件样式 重化控件样式
}
public enum HandleDynamicControls
{
DontPersist,
Persist,
ThrowException
}
}





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>


<%@ Register Namespace="cn.co" TagPrefix="tt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
</asp:RadioButtonList>
<tt:DynamicControlsPanel ControlsWithoutIDs=Persist runat=server ID="pan"></tt:DynamicControlsPanel>
</div>

</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.RadioButtonList1.SelectedValue)
{
case "0":
Control con=Page.LoadControl("0.ascx");
con.ID = "dd";
this.pan.Controls.Clear();
this.pan.Controls.Add(con);
break;
case "1": Control con1 = Page.LoadControl("1.ascx");
con1.ID = "ddd";
this.pan.Controls.Clear();
this.pan.Controls.Add(con1);
break;
}
}
}


浙公网安备 33010602011771号