用vs2010平台,c#写的asp.net网站,动态增加和减少Textbox
用vs2010平台,c#写的asp.net网站,如何实现按一下按钮增加两个TextBox,按另外一个按钮减少后两个TextBox?...
用vs2010平台,c#写的asp.net网站,如何实现按一下按钮增加两个TextBox,按另外一个按钮减少后两个TextBox?
展开
4个回答
展开全部
可以看看一下这个示例:
首先在新建一个WebForm文件,在aspx文件中添加以下控件,以做测试之用,如下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" % >
<!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 >
</head >
<body >
<form id="form1" runat="server" >
<div >
<asp:Panel ID="Panel1" runat="server" >
</asp:Panel >
<br / >
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="Button1_Click" / >
<asp:Button ID="Button2" runat="server" Text="从头删除" OnClick="Button2_Click" / >
<asp:Button ID="Button3" runat="server" Text="从尾删除" OnClick="Button3_Click" / >
指定ID:<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox >
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="删除指定ID的按钮" / >
</div >
</form >
</body >
</html >
一、添加方法:
点击Button1按钮往Panel中添加一个Button按钮:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
this.Panel1.Controls.Add(li);
}
这时候我们发现,当我们第二次点击Button1时,前面添加的Button消失了。这时我们就可以想到,可以使用缓存将用户的每次点击所应该添加的按钮保存起来,于是添加了btnList属性:
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
同时添加将btnList属性中所有Button添加到Panel中的方法:
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
修改Button1的Click事件如下:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
这样我们就可以持续往Panl中添加按钮了。
二、删除方法:
因为我们使用了btnList属性来缓存用户的Button集合,所以删除时,我们只需操作btnList属性即可,如下:
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
.cs文件中的全部代码如下:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
}
}
首先在新建一个WebForm文件,在aspx文件中添加以下控件,以做测试之用,如下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" % >
<!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 >
</head >
<body >
<form id="form1" runat="server" >
<div >
<asp:Panel ID="Panel1" runat="server" >
</asp:Panel >
<br / >
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="Button1_Click" / >
<asp:Button ID="Button2" runat="server" Text="从头删除" OnClick="Button2_Click" / >
<asp:Button ID="Button3" runat="server" Text="从尾删除" OnClick="Button3_Click" / >
指定ID:<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox >
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="删除指定ID的按钮" / >
</div >
</form >
</body >
</html >
一、添加方法:
点击Button1按钮往Panel中添加一个Button按钮:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
this.Panel1.Controls.Add(li);
}
这时候我们发现,当我们第二次点击Button1时,前面添加的Button消失了。这时我们就可以想到,可以使用缓存将用户的每次点击所应该添加的按钮保存起来,于是添加了btnList属性:
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
同时添加将btnList属性中所有Button添加到Panel中的方法:
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
修改Button1的Click事件如下:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
这样我们就可以持续往Panl中添加按钮了。
二、删除方法:
因为我们使用了btnList属性来缓存用户的Button集合,所以删除时,我们只需操作btnList属性即可,如下:
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
.cs文件中的全部代码如下:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
}
}
参考资料: http://www.lmwlove.com/ac/ID515
展开全部
简单的办法,我看到问题,第一个反应时这样解决
添加2个textbox,txt1 txt2
添加2个button , btn1 btn2
添加btn1的响应事件,txt1.visible=true; txt2.visible=true; 这样就添加2个button
btn2的响应事件 就是 txt1.visible=false;这样就是可视化false,相当于减少2个button
上面类似于作弊行为,下面的是 点击button 添加textbox:
Textbox txt1=new Textbox();
txt1.id="txt1"
this.tree.control.add(txt1); //add a textbox to control
代码新建控件必须添加的tree中,语句大概是这样子的,我没打开vs,不知道对不对,你可以在敲代码时通过IDE提示看看Tree这一块的语句怎么写的。
删除的话,同理撒
添加2个textbox,txt1 txt2
添加2个button , btn1 btn2
添加btn1的响应事件,txt1.visible=true; txt2.visible=true; 这样就添加2个button
btn2的响应事件 就是 txt1.visible=false;这样就是可视化false,相当于减少2个button
上面类似于作弊行为,下面的是 点击button 添加textbox:
Textbox txt1=new Textbox();
txt1.id="txt1"
this.tree.control.add(txt1); //add a textbox to control
代码新建控件必须添加的tree中,语句大概是这样子的,我没打开vs,不知道对不对,你可以在敲代码时通过IDE提示看看Tree这一块的语句怎么写的。
删除的话,同理撒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用js点击事件控制textBox的显示和隐藏
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询