栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

Treeview动态添加用户控件传值和取值的实例代码

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Treeview动态添加用户控件传值和取值的实例代码

主要功能:勾选子节点的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;

         }

     }
 }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/127381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号