我升级到2005后
发现动态加载用户控件,在aspx里并不知道控件的类型

解决方案 »

  1.   

    以前在2003里可以这样写
    mycontrol control = (mycontrol)Page.LoadControl("mycontrol.ascx");
    control.param1.....
    control.param2.....
    control.param3.....
    现在在2005下不行了因为在aspx.cs文件里不能识别mycontrol这些类了
      

  2.   

    你的控件.controls.add()利用反射的得到动态加控件的内容。TextBox t=new TextBox();
    t.ID = "tt";
    Panel1.Controls.Add(t);TextBox txt=(TextBox)Panel1.FindControl("tt");
    string str=txt.Text;
      

  3.   

    Control Ctr = Page.LoadControl("MyControl.ascx");
     Ctr.ID = "My1";// 
     PlaceHolder1.Controls.Add(Ctr); 
    用的没问题啊
      

  4.   

    jecray(jecray) 
    控件里的属性你这样能设置吗?id是control的属性
    而不是mycontrol里自定义的属性谁帮我把这个问题解决了再送100分
      

  5.   

    是可以的啊,
    例如:WebUserControl.ascx:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>WebUserControl.ascx.cs: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 WebUserControl : System.Web.UI.UserControl
    {
      string a = "ok";
      public string A
      {
        set {
          a = value;
        }
        get
        {
          return a;
        }
      }
        protected void Page_Load(object sender, EventArgs e)
        {
          TextBox1.Text = A;
        }
      
    }
    调用:<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Page_Load( object sender, EventArgs e )
      {
        WebUserControl a = (WebUserControl)Page.LoadControl("~/WebUserControl.ascx");
        a.A = "xxx";
        form1.Controls.Add(a);
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
       
        </form>
    </body>
    </html>
      

  6.   

    孟子大哥
    你是不是装了WebApplication Project了?
      

  7.   

    你建立一个网站(文件方式即可),
    然后添加UserControl,拷贝代码。在写一个调用即可。
      

  8.   

    可以编译的啊
    你页可以这样 protected void Page_Load( object sender, EventArgs e )
      {
        mxh a = (mxh)Page.LoadControl("~/WebUserControl.ascx");
        a.A = "xxx";
        form1.Controls.Add(a);
      }
    ascx<%@ Control Language="C#" AutoEventWireup="true" ClassName="mxh" CodeFile="WebUserControl.ascx.cs" Inherits="mxh" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    cs:
    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 mxh : System.Web.UI.UserControl
    {
      string a = "ok";
      public string A
      {
        set {
          a = value;
        }
        get
        {
          return a;
        }
      }
        protected void Page_Load(object sender, EventArgs e)
        {
          TextBox1.Text = A;
        }
      
    }