All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP开发网站来看:PHP网站开发方案_php资料_编程技术
PHP实例:PHP制作的网站意见在线反馈表_[PHP教程]
[delphi语法6]delphi中Repeat语句的使用实例
利用XMLHTTP无刷新自动实时更新数据_[XML教程]
自定义阿ASP.NET,CheckBox的设计思路_.net资料_编程技术
Delphi使用ODBC连接SQServer 2000数据库
asp.net乱码处理_[Asp.Net教程]
ASP.NET,MVC,Framework体验(4):控制器_[Asp.Net教程]
网站的ALEXA排名下降的原因及解决方法_JavaScript技术_编程技术
PHP安全配置之实现安全的两个重点_[PHP教程]
Reading number is top 10 pictures
A resort photographed beautiful young woman change clothes process vomiting blood2
A man's favorite things6
Street street fighting
Sora aoi in China1
In 2013 hercules Arnold classic1
恶搞漫画1
这才是真正的人体艺术1
Beauty shocked Japan Tokyo motor show model
看到这个手速,决定过年就让我家猫帮我抢红包了。。
清纯性感的美眉1
Download software ranking
传奇私服架设教程
Boxer's Top ten classic battle6
双旗镇刀客B
Sora aoi, the nurse, uniform ,nursing assistant
在线棋牌游戏3.05版
C++教程第四版
Red cliff
Kung fu panda - the secret of the teacher
艳兽都市
网络管理员第三版
delv published in(发表于) 2014/1/27 6:50:46 Edit(编辑)
动态控件添加终极解决方案_[Asp.Net教程]

动态控件添加终极解决方案_[Asp.Net教程]

动态控件添加终极解决方案_[Asp.Net教程]

动态控件添加解决方案


你可能因为动态添加的控件回传消失而苦烦过吧。
你可能因为动态添加的控件内部事件无法执行而烦过吧。
开始吧。
解决的动态控件添加问题.


做了一个示例:


新建一个类:class1.cs




我在csdn博客上,发现了这个控件。
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>")]


public class DynamicControlsPanel : Panel
{
#region 自定义的属性



[DefaultValue(HandleDynamicControls.DontPersist)]
public HandleDynamicControls ControlsWithoutIDs
{
get
{
if (ViewState["ControlsWithoutIDs"] == null)
return HandleDynamicControls.DontPersist;
else
return (HandleDynamicControls)ViewState["ControlsWithoutIDs"];
}
set { ViewState["ControlsWithoutIDs"] = value; }
}


#endregion 自定义的属性


#region 视图状态管理


protected override void LoadViewState(object savedState)
{
object[] viewState = (object[])savedState;


Pair persistInfo = (Pair)viewState[0];
foreach (Pair pair in (ArrayList)persistInfo.Second)
{
RestoreChildStructure(pair, this);
}


base.LoadViewState(viewState[1]);
}


protected override object SaveViewState()
{


if (HttpContext.Current == null)
return null;


object[] viewState = new object[2];
viewState[0] = PersistChildStructure(this, "C");
viewState[1] = base.SaveViewState();
return viewState;
}


private void RestoreChildStructure(Pair persistInfo, Control parent)
{
Control control;


string[] persistedString = persistInfo.First.ToString().Split(';');


string[] typeName = persistedString[1].Split(':');
switch (typeName[0])
{


case "C":
Type type = Type.GetType(typeName[1], true, true);
try
{
control = (Control)Activator.CreateInstance(type);
}
catch (Exception e)
{
throw new ArgumentException(String.Format("类型 '{0}' 不能恢复状态", type.


ToString()), e);
}
break;
default:
throw new ArgumentException("无法识别的类型.不能恢复状态.");
}


control.ID = persistedString[2];


switch (persistedString[0])
{
case "C":
parent.Controls.Add(control);
break;
}


foreach (Pair pair in (ArrayList)persistInfo.Second)
{
RestoreChildStructure(pair, control);
}
}


private Pair PersistChildStructure(Control control, string controlCollectionName)
{
string typeName;
ArrayList childPersistInfo = new ArrayList();


if (control.ID == null)
{
if (ControlsWithoutIDs == HandleDynamicControls.ThrowException)
throw new NotSupportedException("你必须设置你的ID");
else if (ControlsWithoutIDs == HandleDynamicControls.DontPersist)
return null;
}


typeName = "C:" + control.GetType().AssemblyQualifiedName;


string persistedString = controlCollectionName + ";" + typeName + ";" + control.ID;


if (!(control is UserControl) && !(control is CheckBoxList))
{
for (int counter = 0; counter < control.Controls.Count; counter++)
{
Control child = control.Controls[counter];
Pair pair = PersistChildStructure(child, "C");
if (pair != null)
childPersistInfo.Add(pair);
}
}


return new Pair(persistedString, childPersistInfo);
}
#endregion 视图状态管理


#region 重化控件样式
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
}
//
#endregion 重化控件样式
}


public enum HandleDynamicControls
{
DontPersist,
Persist,
ThrowException
}
}


default7.aspx页面代码




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


<%@ Register Namespace="cn.co" TagPrefix="tt" %>





无标题页






0
1









default7.aspx.cs页面




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;
}
}
}


还有两个o.ascx,1.ascs页面。
代码分别为
0.ascx
前台



<%@ Control Language="C#" AutoEventWireup="true" CodeFile="0.ascx.cs" Inherits="_0" %>
0控件。

onClick="Button1_Click" Text="0控件" />


后台


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 _0 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("0控件");
}
}


1.ascx
前台
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="1.ascx.cs" Inherits="_1" %>
1控件。onClick="Button1_Click" Text="1控件" />
后台:
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 _1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("1控件");
}
}


测试结果如图:
打开的页面:




当我的点击时:





当我们刷新时

结果还是


来源:suiqirui19872005的cnblogs







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.