现在的位置: 首页 > 综合 > 正文

动态增加控件。转

2011年07月10日 ⁄ 综合 ⁄ 共 1077字 ⁄ 字号 评论关闭
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
 
if(!IsPostBack) 
 ViewState[
"Count"]=0;
 
else 
 
for(int i=1;i<=(int)ViewState["Count"];i++)
 
{
 addproduct(i.ToString());
 }

}

 
void addproduct(string num)
 
{
 LiteralControl mylabel;
 TextBox mybox 
= new TextBox();
 mylabel 
= new LiteralControl();
 mylabel.Text 
= "<p>" + "产品:" + num;
 PlaceHolder1.Controls.Add(mylabel);
 mybox 
= new TextBox();
 mybox.ID 
= "txtProduct" + num;
 PlaceHolder1.Controls.Add(mybox);
 }

protected void add_btn(object sender, EventArgs e)
{
 
int count =(int)ViewState["Count"];
 count
++;
 ViewState[
"Count"= count;
 addproduct(ViewState[
"Count"].ToString());
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 
<title>动态添加控件</title>
</head>
<body style="font-size: 12px;">
 
<form id="form1" runat="server">
 
<div>
 
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
 
<br />
 
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="add_btn" />
 
</form>
</body>
</html>

  Server.MapPath所在命名空间
 System.Web.HttpContext.Current.Server.MapPath

抱歉!评论已关闭.