动态添加用户控件实例

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>

posted @ 2006-09-07 18:04  冰封的心  阅读(681)  评论(2编辑  收藏  举报