Web动态添加控件

好像问动态添加控件的人很多,问题大多集中
(1)动态添加的按钮不能提交
(2)动态添加的文本框取不到值
(3)动态添加的控件页面刷新后消失
(4)动态添加的控件页面刷新重复添加
(5)动态添加的控件不知道怎么移除
问题百出,但是其实这些都是非常简单的,可能疏忽了一点两点,补充以前写的2篇文章(以前写的比较乱)
这个页面实现:
点击添加按钮-》添加一个文本框一个提交按钮-》点击提交按钮输出文本框值
点击删除按钮(就是前面那个添加按钮)-》移除文本框和提交按钮

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;

namespacecsdn2
{
/**
///WebForm65的摘要说明。
///
publicclassWebForm65:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonButton1;

privatevoidPage_Load(objectsender,System.EventArgse)
{
if(Page.IsPostBack)
{
if(ViewState["adduc"]!=null)
{
adduc();
}
}
}

Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///
privatevoidInitializeComponent()
{
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);

}
#endregion

privatevoidButton1_Click(objectsender,System.EventArgse)
{
if(ViewState["adduc"]==null)
{
adduc();
ViewState["adduc"]=1;
}
else
{
deluc();
ViewState["adduc"]=null;
}
}

privatevoidButton2_Click(objectsender,System.EventArgse)
{
TextBoxt=(TextBox)Page.FindControl("t");
if(t!=null)Response.Write(t.Text);
}

publicvoidadduc()
{
TextBoxt=newTextBox();
t.ID="t";
Page.Controls[1].Controls.Add(t);
this.Button1.Text="删除";
Buttonb=newButton();
b.Text="提交";
b.ID="b";
b.Click+=newSystem.EventHandler(this.Button2_Click);
Page.Controls[1].Controls.Add(b);
}

publicvoiddeluc()
{
Page.Controls[1].Controls.Remove(Page.FindControl("t"));
Page.Controls[1].Controls.Remove(Page.FindControl("b"));
this.Button1.Text="添加";
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值