用户控件ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddAttribute.ascx.cs" Inherits="Controls_AddAttribute" %>
<center>
<table border="1" cellpadding="0" cellspacing="2" width="100%">
<tr>
<td style="width: 189px; height: 25px" align="right">
属性名称:</td>
<td style="height: 25px" align="left">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 189px; height: 25px" align="right">
属性说明:</td>
<td style="height: 25px" align="left">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</center>
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 Controls_AddAttribute : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Text = "ok";
this.TextBox2.Text = "A";
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Mall_AttributeManage.aspx.cs" Inherits="Manage_Mall_Mall_AttributeManage" %>
<%@ Register Src="http://www.cnblogs.com/Controls/AddAttribute.ascx" TagName="AddAttribute" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>属性管理</title>
<link rel="stylesheet" href="../css/common.css" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<table border="1" cellpadding="0" cellspacing="2" width="100%">
<tr>
<td colspan="2" style="background-color:#507CD1;">
<asp:Label ID="Label1" runat="server" Text="Label" Width="705px"></asp:Label></td>
</tr>
<tr>
<td style="width: 189px; height: 25px" align="right">
选择父类:</td>
<td style="height: 25px" align="left">
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
</asp:DropDownList></td>
</tr>
<asp:Panel ID="AddPanel" runat="server" Width="100%"></asp:Panel>
</table>
<table border="1" cellpadding="0" cellspacing="2" width="100%">
<tr>
<td style="width: 275px" align="right"><asp:Button ID="btnSubmit" runat="server" CssClass="btn" Height="26px" Text="提 交" Width="80px" OnClick="btnAdd_Click" /></td>
<td style="width: 61px"></td>
<td align="left"><asp:Button ID="btnAdd" runat="server" CssClass="btn" Height="26px" Text="添加属性" Width="80px" /></td>
</tr>
</table>
</center>
</div>
<asp:HiddenField ID="hfCount" runat="server" Value="0" />
</form>
</body>
</html>
页面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 Manage_Mall_Mall_AttributeManage : System.Web.UI.Page
{
private int _count = 0;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// 取得以前已创建控件的个数
if (!String.IsNullOrEmpty(this.Request["hfCount"]))
{
_count = Convert.ToInt32(this.Request["hfCount"]);
}
// 假如按下“Add”按钮,那么count加一
string target = this.Request["btnAdd"];
//Response.Write(this.Request["hfCount"] + "//" + target);
if (target == "添加属性")
{
_count++;
}
// 动态创建控件
for (int i = 0; i < _count; i++)
{
// 这里以TextBox为例,实际上需要创建的是WebPartZone
//TextBox newTextbox = new TextBox();
//newTextbox.ID = "TXT" + i.ToString();
//this.AddPanel.Controls.Add(newTextbox);
Control c = LoadControl("http://www.cnblogs.com/Controls/AddAttribute.ascx");
c.ID = "uc" + i.ToString();
this.AddPanel.Controls.Add(c);
//Add();
}
}
protected void Page_Load(object sender, EventArgs e)
{
hfCount.Value = _count.ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
for (int i = 0; i < _count; i++)
{
TextBox txt1=(TextBox)this.AddPanel.FindControl("uc"+i.ToString()).FindControl("TextBox1");
TextBox txt2=(TextBox)this.AddPanel.FindControl("uc"+i.ToString()).FindControl("TextBox2");
this.Label1.Text += "/"+txt1.Text.Trim() + "///" + txt2.Text.Trim();
}
}
}
前台页面aspx
用户控件cs