当前位置:编程学习 > C#/ASP.NET >>

动态添加的 ascx 控件 怎么给它传值啊!

动态添加的 ascx 控件 怎么给它传值啊!  

wc.ascx 里面有个 name 属性


Control myControl = Page.LoadControl("~/control/wc.ascx");
       
PlaceHolder1.Controls.Add(myControl);
这里添加了 不知道怎么给它传值谢谢! --------------------编程问答-------------------- mark --------------------编程问答-------------------- 为它添加一个属性就可以了 --------------------编程问答--------------------

control_wc myControl = LoadControl("~/control/wc.ascx") as control_wc;
      
if(myControl !=null)
{
   myControl.xxx="";
}
PlaceHolder1.Controls.Add(myControl); 


control_wc 为ASCX 控件的类名

xxx为 它的属性 --------------------编程问答-------------------- 在ascx 控件里放一个 public 的参数就可以了

例如放个


private int _age=18;

public int Age
{
   get {return _age;}
   set {_age = (value>120) ? 18 : value }
}
--------------------编程问答-------------------- 就像使用类的属性一样引用就成,3楼的方法可行!!! --------------------编程问答-------------------- 为这个用户控件添加属性才是正解. --------------------编程问答-------------------- ding... --------------------编程问答--------------------  找不到类型或命名空间名称“control_wc”(是否缺少 using 指令或程序集引用?)
--------------------编程问答-------------------- 赞同添加属性。在调用程序的page_load 里给属性赋值。我就是那样做的 --------------------编程问答--------------------
引用 8 楼 hy86459 的回复:
找不到类型或命名空间名称“control_wc”(是否缺少 using 指令或程序集引用?) 


你 的用户控件的类名叫什么 --------------------编程问答-------------------- 用属性的方式 --------------------编程问答--------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class temp_uc : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}


temp_uc  就是类名 --------------------编程问答-------------------- string control = Request.Form["Control"] == null ? "Rss" : Request.Form["Control"];
string name = Request.Form["Name"] == null ? "模块" : Request.Form["Name"];
int column = Request.Form["Column"] == null ? 3 : int.Parse(Request.Form["Column"]);
Control userControl = this.LoadControl(control);
        this.Controls.Add(userControl);
        if (userControl is PartialCachingControl)
        {
            PartialCachingControl uc = userControl as PartialCachingControl;
            if (uc.CachePolicy != null) userControl = uc.CachedControl;
        }   
        if (userControl != null)
        {
            Type controlType;
            if(userControl.GetType().BaseType.GetProperty("name")!=null)
                 controlType = userControl.GetType().BaseType;
            else
                controlType = userControl.GetType().BaseType.BaseType;

            PropertyInfo field = controlType.GetProperty("name");

            if (field != null)
            {
                field.SetValue(userControl, name, null);
            }

            field = controlType.GetProperty("column");

            if (field != null)
            {
                field.SetValue(userControl, column, null);
            }
        }
        userControl.DataBind();
  
*****************************************************************************
河南地区求职.net程序开发,底薪3000低于勿扰。
欢迎长期技术学习交流朋友,尽力组建一只技术研究的网络团体,研发资料大家共享不外放,新手勿扰。
工作QQ:303864496 
http://losingrose.cnblogs.com/
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,