Control c = LoadControl("用户控件.ascx");
this.Controls.Add(c);orPlaceHolder1.Controls.Add(c);orForm1.Controls.Add(c);

解决方案 »

  1.   

    请问楼上的大哥:第一行的Control是不是在.ascx.cs中定义的用户控件类?
      

  2.   

    no, Control is just a class name, TemplateControl.LoadControl method:
    public Control LoadControl(string virtualPath);
      

  3.   

    我在用户控件中定义了一个DataGrid,怎么动态加载时无法显示,请指教!
    用户控件代码如下:
    GoodBook.ascx
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="GoodBook.ascx.cs" Inherits="ElectronicBookShop.GoodBook" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <FONT face="宋体"></FONT><span style="FONT-SIZE: 8pt; FONT-FAMILY: verdana">
    <asp:datagrid id="GoodBookGrid" GridLines="vertical" Font-Size="8pt" Font-Name="Verdana" CellPadding="2"
    BorderColor="lightgray" BorderWidth="1" RunAt="server">
    <HeaderStyle BackColor="maroon" ForeColor="white" HorizontalAlign="center" />
    <ItemStyle BackColor="white" ForeColor="black" />
    <AlternatingItemStyle BackColor="beige" ForeColor="black" />
    </asp:datagrid> </span>
    GoodBook.ascx.cs
    public class GoodBook : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.DataGrid GoodBookGrid; Unit width;
    public Unit Width
    {
    get
    {
    return width;
    }
    set
    {
    width=value;
    }
    } private void Page_Load(object sender, System.EventArgs e)
    {
    if(width!=Unit.Empty)
    GoodBookGrid.Width=width;
    //将书名和价钱填入到DataSet中
    DataSet ds=new DataSet();
    DataTable book=new DataTable("GoodBook");
    ds.Tables.Add(book); //初始化book表
    DataColumn NameCol =new DataColumn("Name",typeof(string));
    DataColumn PriceCol=new DataColumn("Price",typeof(string));
    book.Columns.Add(NameCol);
    book.Columns.Add(PriceCol); DataRow row =book.NewRow();
    row["Name"]="C Sharp";
    row["Price"]=String.Format("{0:c}",60);
    book.Rows.Add(row); row =book.NewRow();
    row["Name"]="Visual C++";
    row["Price"]=String.Format("{0:c}",30);
    book.Rows.Add(row); row =book.NewRow();
    row["Name"]="Thinking in Java";
    row["Price"]=String.Format("{0:c}",50);
    book.Rows.Add(row);
    row =book.NewRow();
    row["Name"]="Thinking in C++";
    row["Price"]=String.Format("{0:c}",50);
    book.Rows.Add(row);
    row =book.NewRow();
    row["Name"]="DotNet FrameWork";
    row["Price"]=String.Format("{0:c}",50);
    book.Rows.Add(row); //将DataGrid绑定到DataSet
    GoodBookGrid.DataSource=ds;
    GoodBookGrid.DataBind(); }
    }
      

  4.   

    did you hook up Page_Load with Load event, for example #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    } /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    or simply change <%@ Control Language="c#" AutoEventWireup="false" ...%>
    ===>
    <%@ Control Language="c#" AutoEventWireup="true" ...%>
      

  5.   

    The problem has been cleared up.Thank you very much.By the way ,I want to make friend with you ,can I have the honor?My Email is [email protected],thank you.
      

  6.   

    hehe好像楼主不知道思归是谁哦@:)
      

  7.   

    思归大侠请帮帮忙!我在web 页面中动态加载了一用户控件后,同时又设了一Button(对应一消息处理函数),当运行后,单击button产生回发时,加载的用户控件不见了,不明白是什么原因。请指教!
    谢谢!
      

  8.   

    document.findControl("Form1").Controls.Add(LoadControl("mycontrol.ascx")