主要功能:勾选子节点的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" %>
这是后台代码: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 = https://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 = https://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" %>
当前节点的值:
这是用户控件的后台: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 = https://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 = https://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;
}
}
}



