今天做了很好玩的树,是treeview与用户控件之间进行交互先看效果:
主要功能:勾选子节点的checkbox,右边会动态加载该节点的信息,出现TextBox让用户填写节点的值,点击保存按钮将文本框的值保存到对应的节点。
里面涉及到了asp执行ascx页面里的事件,并取值。
这是前台的代码:CustomXMLmanager2.aspx
![](https://img7.php1.cn/3cdc5/d062/711/4f173ab6f98dcecf.gif)
![](https://img7.php1.cn/3cdc5/d062/711/debb3952ce5401ac.gif)
1 <%&#64; Page Language&#61;"C#" AutoEventWireup&#61;"true" CodeBehind&#61;"CustomXMLmanager2.aspx.cs" Inherits&#61;"usexml.CustomXMLmanager2" %>
2
3 <%&#64; Register src&#61;"Custom2.ascx" tagname&#61;"Custom2" tagprefix&#61;"uc1" %>
4
5 "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 "http://www.w3.org/1999/xhtml">
8 "server">
9
10
11
12
13
30 "form1" runat&#61;"server">
31
32
33
34
35
36
"30%"> 37 38 39 40 ShowLines&#61;"True" 41 ViewStateMode&#61;"Enabled"> 42 43 44 45 46 47 48 49 50 51 52 53 HorizontalPadding&#61;"0px" NodeSpacing&#61;"0px" VerticalPadding&#61;"0px" /> 54 55 56 HorizontalPadding&#61;"0px" VerticalPadding&#61;"0px" /> 57 58 59 60 61 62 63 | "background-color: #008080; width: 2px"> | 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
83
84
85
86
87
88
89
90
91 οnclick&#61;"Button1_Click" />
92
93
94
这是后台代码:CustomXMLmanager2.aspx.cs
![](https://img7.php1.cn/3cdc5/d062/711/4f173ab6f98dcecf.gif)
![](https://img7.php1.cn/3cdc5/d062/711/debb3952ce5401ac.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Reflection;
8
9 namespace usexml
10 {
11 public partial class CustomXMLmanager2 : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15
16 if (IsPostBack)
17 {
18
19
20 //if (ViewState["node"] !&#61; null)
21 //{
22 // nodes1();
23
24 //}
25
26 nodes1();
27
28
29 }
30
31
32
33
34
35
36
37 TreeView1.Attributes.Add("onclick", "postBackByObject()");
38
39 }
40
41
42 private void nodes1()
43 {
44 int tg &#61; 0;
45 foreach (TreeNode nod in TreeView1.CheckedNodes)
46 {
47
48 nod.Target &#61; tg.ToString();
49 Custom2 uc &#61; (Custom2)LoadControl("Custom2.ascx");
50 uc.Nodname &#61; nod.Text;
51 uc.Nodvalue &#61; nod.Value;
52
53 uc.Nodetag &#61; nod.Target;
54
55 PlaceHolder1.Controls.Add(uc);
56
57 tg&#43;&#43;;
58
59 }
60
61
62 }
63
64
65 protected void Button1_Click(object sender, EventArgs e)
66 {
67 for (int i &#61; 0; i
69 UserControl uc2 &#61; (UserControl)PlaceHolder1.Controls[i];
70
71 Type ucType &#61; uc2.GetType();
72
73 //用MethodInfo类来获取用户控件中的方法&#xff0e;
74
75 MethodInfo UcMethod &#61; ucType.GetMethod("GetText");// Button1_Click控件中的方法。
76
77 //在此处页面的方法中执行用户控件中的方法&#xff0e;
78
79 object[] argumentArrray &#61; new object[0];
80
81 UcMethod.Invoke(uc2, new object[0]);//调用用户控件中的方法。此处执行了&#xff01;&#xff01;。
82
83 foreach (TreeNode nod in TreeView1.CheckedNodes)
84 {
85
86
87 PropertyInfo UctextName &#61; ucType.GetProperty("PicName");
88 PropertyInfo tag &#61; ucType.GetProperty("Nodetag");
89 if (nod.Target &#61;&#61; tag.GetValue(uc2, null).ToString())
90 {
91
92 nod.Value &#61; UctextName.GetValue(uc2, null).ToString();//获取了上传的文件名信息。并显示在 page 页面上。
93
94 }
95
96
97 }
98
99
100 }
101
102 }
103
104 }
105 }
这是用户控件的前台:Custom2.ascx
![](https://img7.php1.cn/3cdc5/d062/711/4f173ab6f98dcecf.gif)
![](https://img7.php1.cn/3cdc5/d062/711/debb3952ce5401ac.gif)
1 <%&#64; Control Language&#61;"C#" AutoEventWireup&#61;"true" CodeBehind&#61;"Custom2.ascx.cs" Inherits&#61;"usexml.Custom2" %>
2
3
4
5 当前节点的值:
6
7
这是用户控件的后台:Custom2.ascx.cs
![](https://img7.php1.cn/3cdc5/d062/711/4f173ab6f98dcecf.gif)
![](https://img7.php1.cn/3cdc5/d062/711/debb3952ce5401ac.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 namespace usexml
9 {
10 public partial class Custom2 : System.Web.UI.UserControl
11 {
12
13
14 private string nodname &#61; "";
15 public string nodvalue &#61; "";
16 private string nodtag &#61; "";
17 private string picname &#61; "";
18 public string Nodname
19 {
20
21
22 get
23 {
24
25 return nodname;
26
27 }
28
29 set
30 {
31
32
33 nodname &#61; value;
34
35 }
36
37
38 }
39
40 public string Nodvalue
41 {
42
43
44 get
45 {
46
47 return nodvalue;
48
49 }
50
51 set
52 {
53
54
55 nodvalue &#61; value;
56
57 }
58
59
60 }
61
62 public string Nodetag
63 {
64
65
66 get
67 {
68
69 return nodtag;
70
71 }
72
73 set
74 {
75
76
77 nodtag &#61; value;
78
79 }
80
81
82 }
83
84 public string PicName
85 {
86
87 get { return picname; }
88
89 set { picname &#61; value; }
90
91 }
92
93 protected void Page_Load(object sender, EventArgs e)
94 {
95 Label1.Text &#61; nodname&#43;"&#xff1a;";
96
97 Label2.Text &#61; nodvalue;
98
99
100 }
101
102 public void GetText()
103 {
104
105
106 picname &#61; TextBox1.Text;
107 TextBox1.Text &#61; "";
108 Label2.Text &#61; picname;
109
110
111
112 }
113
114
115 }
116 }
主要功能已实现&#xff0c;下一步我要将其结合到XML去&#xff0c;实现对XML文档的自定义扩展。