Treeview動態添加使用者控制項傳值和取值的執行個體代碼

來源:互聯網
上載者:User

主要功能:勾選子節點的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 = http://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 = http://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 = http://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 = http://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;

}

}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.