Treeview動態(tài)添加用戶控件傳值和取值的實例代碼
主要功能:勾選子節(jié)點的checkbox,右邊會動態(tài)加載該節(jié)點的信息,出現(xiàn)TextBox讓用戶填寫節(jié)點的值,點擊保存按鈕將文本框的值保存到對應的節(jié)點。
里面涉及到了asp執(zhí)行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">
// 點擊復選框時觸發(fā)事件
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////"海洋信息數(shù)據(jù)集">
<asp:TreeNode Text="名字" Value=http://www.cnblogs.com/panan/archive////"數(shù)據(jù)名稱"></asp:TreeNode>
<asp:TreeNode Text="性別" Value=http://www.cnblogs.com/panan/archive////"數(shù)據(jù)格式"></asp:TreeNode>
<asp:TreeNode Text="年齡" Value=http://www.cnblogs.com/panan/archive////"數(shù)據(jù)摘要"></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 = http://www.dbjr.com.cn/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控件中的方法。
//在此處頁面的方法中執(zhí)行用戶控件中的方法.
object[] argumentArrray = new object[0];
UcMethod.Invoke(uc, new object[0]);//調(diào)用用戶控件中的方法。此處執(zhí)行了?。?。
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 = http://www.dbjr.com.cn/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>
當前節(jié)點的值:<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 = http://www.dbjr.com.cn/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 = http://www.dbjr.com.cn/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;
}
}
}
- ASP.NET中使用TreeView顯示文件的方法
- ASP.NET使用TreeView顯示文件的方法
- treeview遞歸綁定的兩種方法
- c#的treeview綁定和獲取值的方法
- JS控件ASP.NET的treeview控件全選或者取消(示例代碼)
- 關(guān)于ASP.NET中TreeView用法的一個小例子
- C# TreeView讀取數(shù)據(jù)庫簡單實例
- ASP.NET TreeView讀取數(shù)據(jù)庫實例
- ASP.NET實現(xiàn)TreeView的XML數(shù)據(jù)源綁定實例代碼
- asp.net TreeView與XML三步生成列表樹
- TreeView無刷新獲取text及value實現(xiàn)代碼
- 在WCF數(shù)據(jù)訪問中使用緩存提高Winform字段中文顯示速度的方法
- Winform中Treeview實現(xiàn)按需加載的方法
相關(guān)文章
C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
這篇文章主要介紹了C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法,涉及C#針對注冊表的讀取與監(jiān)控技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲原理分析
這篇文章主要介紹了C# 指針 內(nèi)存控制 Marshal 內(nèi)存數(shù)據(jù)存儲原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02