动态增加控件。转

<% @ 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

你可能感兴趣的:(动态)