Treeview动态添加用户控件 取值和传值(第二种样式)

功能效果图:

 

功能实现:勾选节点复选框,右边动态加载用户控件,点击赋值按钮,将文本值赋值给节点

 

前台代码CustomXMLManager.aspx

View Code
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomXMLManager.aspx.cs" Inherits="usexml.CustomXMLManager" %>
2
3
4
5
6
7 <%@ Register src="Custom1.ascx" tagname="Custom1" tagprefix="uc1" %>
8
9
10
11
12
13 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
14
15 <html xmlns="http://www.w3.org/1999/xhtml">
16 <head runat="server">
17 <title></title>
18 </head>
19 <body>
20 <script language="javascript" type="text/javascript">
21
22 // 点击复选框时触发事件
23
24 function postBackByObject() {
25
26 var o = window.event.srcElement;
27
28 if (o.tagName == "INPUT" && o.type == "checkbox") {
29
30 __doPostBack("", "");
31
32 }
33
34 }
35
36 </script>
37
38 <form id="form1" runat="server">
39 <div>
40 <asp:ScriptManager ID="ScriptManager1" runat="server">
41 </asp:ScriptManager>
42 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
43 <ContentTemplate>
44 <table width="100%" style="border: 3px dotted #008080"><tr><td width="50%">
45
46
47 <asp:TreeView ID="TreeView1" runat="server" ImageSet="Simple" ShowCheckBoxes="Leaf"
48 ShowLines="True" ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged"
49 onselectednodechanged="TreeView1_SelectedNodeChanged"
50 ViewStateMode="Enabled">
51 <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
52 <Nodes>
53 <asp:TreeNode Text="用户信息" Value="海洋信息数据集">
54 <asp:TreeNode Text="姓名" Value="数据名称" Target="1"></asp:TreeNode>
55 <asp:TreeNode Text="性别" Value="数据格式" Target="2"></asp:TreeNode>
56 <asp:TreeNode Text="帅不帅" Value="数据摘要" Target="3"></asp:TreeNode>
57 </asp:TreeNode>
58 </Nodes>
59 <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
60 HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
61 <ParentNodeStyle Font-Bold="False" />
62 <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
63 HorizontalPadding="0px" VerticalPadding="0px" />
64 </asp:TreeView>
65
66
67
68
69
70 </td>
71 <td>
72
73
74
75
76
77 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
78
79
80
81
82
83
84
85 </td>
86
87 </tr></table>
88 <div>
89
90 </div>
91 </ContentTemplate>
92 </asp:UpdatePanel>
93
94 </div>
95
96 </form>
97
98 </body>
99 </html>

 

后台代码CustomXMLManager.aspx.cs

View Code
  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.Web.UI.WebControls.WebParts;
8 using System.Web.UI.HtmlControls;
9
10 namespace usexml
11 {
12 public partial class CustomXMLManager : System.Web.UI.Page
13 {
14
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 if (IsPostBack)
19 {
20
21
22 //if (ViewState["node"] != null)
23 //{
24 // nodes1();
25
26 //}
27
28 nodes1();
29
30 }
31
32 TreeView1.Attributes.Add("onclick", "postBackByObject()");
33
34
35
36
37
38 }
39
40 public void SetLabel(string str,string str2)
41 {
42
43 foreach (TreeNode nod in TreeView1.CheckedNodes)
44 {
45 if (nod.Target == str2)
46
47 {
48
49 nod.Value = str;
50
51 }
52
53
54 }
55
56 }
57
58 protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
59 {
60
61
62
63
64
65
66
67
68
69
70
71 }
72
73 private void nodes1()
74 {
75
76 foreach (TreeNode nod in TreeView1.CheckedNodes)
77 {
78
79 Custom1 uc = (Custom1)LoadControl("Custom1.ascx");
80 uc.Nodname = nod.Text;
81 uc.Nodvalue = nod.Value;
82 uc.Nodetag = nod.Target;
83 PlaceHolder1.Controls.Add(uc);
84
85
86
87 }
88
89
90 }
91
92 protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
93 {
94
95
96
97 }
98
99
100
101
102
103
104 }
105 }

 

用户控件前台代码Custom1.ascx

View Code
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Custom1.ascx.cs" Inherits="usexml.Custom1" %>
2 <br />
3 <asp:Label ID="Label1" runat="server" ForeColor="#006666"
4 Text="Label"></asp:Label>
5 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
6
7 <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="赋值" />
8
9
10 <asp:Label ID="Label2" runat="server" Text="Label" Visible="False"></asp:Label>
11 当前节点的值为:<asp:Label ID="Label3" runat="server" Text="Label" ForeColor="#003366"></asp:Label>

 

用户控件后台代码Custom1.ascx.cs

 

View Code
  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 Custom1 : System.Web.UI.UserControl
12 {
13
14 private string nodname="";
15 public string nodvalue="";
16 private string nodtag = "";
17 public string Nodname
18 {
19
20
21 get
22 {
23
24 return nodname;
25
26 }
27
28 set
29 {
30
31
32 nodname = value;
33
34 }
35
36
37 }
38
39 public string Nodvalue
40 {
41
42
43 get
44 {
45
46 return nodvalue;
47
48 }
49
50 set
51 {
52
53
54 nodvalue = value;
55
56 }
57
58
59 }
60
61 public string Nodetag
62 {
63
64
65 get
66 {
67
68 return nodtag;
69
70 }
71
72 set
73 {
74
75
76 nodtag = value;
77
78 }
79
80
81 }
82 protected void Page_Load(object sender, EventArgs e)
83 {
84
85 Label1.Text = nodname;
86 Label3.Text = nodvalue;
87 Label2.Text = nodtag;
88
89
90 }
91
92 protected void Button1_Click(object sender, EventArgs e)
93 {
94
95
96 System.Web.UI.Page p = this.Page;
97 Type pageType = p.GetType();
98 MethodInfo mi = pageType.GetMethod("SetLabel");
99 mi.Invoke(p, new object[] { TextBox1.Text, Label2.Text } );
100
101 Label3.Text = TextBox1.Text;
102 TextBox1.Text = "";
103
104 }
105
106
107
108
109 }
110 }


哇擦!!!好累哦!!!

转载于:https://www.cnblogs.com/panan/archive/2011/12/28/2304869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值