Treeview动态添加用户控件传值和取值的实例代码
主要功能:勾选子节点的checkbox,右边会动态加载该节点的信息,出现textbox让用户填写节点的值,点击保存按钮将文本框的值保存到对应的节点。
里面涉及到了asp执行ascx页面里的事件,并取值。
这是前台的代码:customxmlmanager.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="customxmlmanager.aspx.cs" inherits="usexml.customxmlmanager" %>
<%@ register src=http://www.cnblogs.com/panan/archive////"custom.ascx" tagname="custom" tagprefix="uc" %>
<!doctype html public "-//wc//dtd xhtml .0 transitional//en" "http://www.w.org/tr/xhtml/dtd/xhtml-transitional.dtd">
<html xmlns="http://www.w.org//xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script language="javascript" type="text/javascript">
// 点击复选框时触发事件
function postbackbyobject() {
var o = window.event.srcelement;
if (o.tagname == "input" && o.type == "checkbox") {
__dopostback("", "");
}
}
</script>
<form id="form" runat="server">
<div>
<asp:scriptmanager id="scriptmanager" runat="server">
</asp:scriptmanager>
<asp:updatepanel id="updatepanel" runat="server">
<contenttemplate>
<table width="%" style="border: px dotted #00"><tr><td width="%">
<asp:treeview id="treeview" runat="server" imageset="simple" showcheckboxes="leaf"
showlines="true"
viewstatemode="enabled">
<hovernodestyle font-underline="true" forecolor="#dd" />
<nodes>
<asp:treenode text="个人信息" value=http://www.cnblogs.com/panan/archive////"海洋信息数据集">
<asp:treenode text="名字" value=http://www.cnblogs.com/panan/archive////"数据名称"></asp:treenode>
<asp:treenode text="性别" value=http://www.cnblogs.com/panan/archive////"数据格式"></asp:treenode>
<asp:treenode text="年龄" value=http://www.cnblogs.com/panan/archive////"数据摘要"></asp:treenode>
<asp:treenode text="帅不帅" value=http://www.cnblogs.com/panan/archive////"帅不帅"></asp:treenode>
<asp:treenode text="漂不漂亮" value=http://www.cnblogs.com/panan/archive////"漂不漂亮"></asp:treenode>
</asp:treenode>
</nodes>
<nodestyle font-names="tahoma" font-size="pt" forecolor="black"
horizontalpadding="0px" nodespacing="0px" verticalpadding="0px" />
<parentnodestyle font-bold="false" />
<selectednodestyle font-underline="true" forecolor="#dd"
horizontalpadding="0px" verticalpadding="0px" />
</asp:treeview>
</td>
<td style="background-color: #00; width: px"></td>
<td>
<asp:placeholder id="placeholder" runat="server"></asp:placeholder>
</td>
</tr></table>
<div>
</div>
</contenttemplate>
</asp:updatepanel>
</div>
<div align="center"><asp:button id="button" runat="server" text="保存"
onclick="button_click" /></div>
</form>
</body>
</html>
这是后台代码:customxmlmanager.aspx.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.reflection;
namespace usexml
{
public partial class customxmlmanager : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (ispostback)
{
//if (viewstate["node"] != null)
//{
// nodes();
//}
nodes();
}
treeview.attributes.add("onclick", "postbackbyobject()");
}
private void nodes()
{
int tg = 0;
foreach (treenode nod in treeview.checkednodes)
{
nod.target = tg.tostring();
custom uc = (custom)loadcontrol("custom.ascx");
uc.nodname = nod.text;
uc.nodvalue = //www.jb51.net/panan/archive////nod.value;
uc.nodetag = nod.target;
placeholder.controls.add(uc);
tg++;
}
}
protected void button_click(object sender, eventargs e)
{
for (int i = 0; i < placeholder.controls.count; i++)
{
usercontrol uc = (usercontrol)placeholder.controls[i];
type uctype = uc.gettype();
//用methodinfo类来获取用户控件中的方法.
methodinfo ucmethod = uctype.getmethod("gettext");// button_click控件中的方法。
//在此处页面的方法中执行用户控件中的方法.
object[] argumentarrray = new object[0];
ucmethod.invoke(uc, new object[0]);//调用用户控件中的方法。此处执行了!!。
foreach (treenode nod in treeview.checkednodes)
{
propertyinfo uctextname = uctype.getproperty("picname");
propertyinfo tag = uctype.getproperty("nodetag");
if (nod.target == tag.getvalue(uc, null).tostring())
{
nod.value = //www.jb51.net/panan/archive////uctextname.getvalue(uc, null).tostring();//获取了上传的文件名信息。并显示在 page 页面上。
}
}
}
}
}
}
这是用户控件的前台:custom2.ascx
<%@ control language="c#" autoeventwireup="true" codebehind="custom2.ascx.cs" inherits="usexml.custom2" %>
<div>
<asp:label id="label1" runat="server" text="label" forecolor="#006666"></asp:label>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
当前节点的值:<asp:label id="label2" runat="server" text="label" forecolor="#003366"></asp:label>
</div>
这是用户控件的后台:custom2.ascx.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace usexml
{
public partial class custom2 : system.web.ui.usercontrol
{
private string nodname = "";
public string nodvalue = //www.jb51.net/panan/archive/2011/12/28/"";
private string nodtag = "";
private string picname = "";
public string nodname
{
get
{
return nodname;
}
set
{
nodname = value;
}
}
public string nodvalue
{
get
{
return nodvalue;
}
set
{
nodvalue = //www.jb51.net/panan/archive/2011/12/28/value;
}
}
public string nodetag
{
get
{
return nodtag;
}
set
{
nodtag = value;
}
}
public string picname
{
get { return picname; }
set { picname = value; }
}
protected void page_load(object sender, eventargs e)
{
label1.text = nodname+":";
label2.text = nodvalue;
}
public void gettext()
{
picname = textbox1.text;
textbox1.text = "";
label2.text = picname;
}
}
}