asp.net,动态添加控件,并且删除指定添加的控件
为什么我的删除事件,执行不到,以下是我的代码前台:<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server...
为什么我的删除事件,执行不到,以下是我的代码
前台:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td align="left">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td align="right">
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Table ID="Table1" runat="server">
</asp:Table>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button2_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
static ArrayList al = new ArrayList();
static int tag = 0;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string target = this.Request["Button1"];
if (target == "添加")
{
if (al.Count > 0)
{
tag++;
}
else
{
tag = 1;
}
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TextBox tb = new TextBox();
Button bt = new Button();
tb.ID = "text" + tag.ToString();
bt.ID = "delete" + tag.ToString();
tr.ID = "row" + tag.ToString();
bt.Text = "删除";
bt.CommandName = tag.ToString();
bt.Command += new CommandEventHandler(delete_Command);
bt.Click += new EventHandler(delete_Click);
tc1.Controls.Add(tb);
tc2.Controls.Add(bt);
tc1.HorizontalAlign = HorizontalAlign.Right;
tc2.HorizontalAlign = HorizontalAlign.Left;
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
al.Add(tr);
}
for (int i = 0; i < al.Count; i++)
{
Table1.Rows.Add((TableRow)al[i]);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void delete_Command(object sender, CommandEventArgs e)
{
for (int i = 0; i < al.Count; i++)
{
string commandname = ((Button)((TableCell)((TableRow)al[i]).Cells[1]).Controls[0]).CommandName;
if (e.CommandName == commandname)
{
al.RemoveAt(i);
break;
}
}
}
protected void delete_Click(object sender, EventArgs e)
{
Response.Write("aaaaaa");
}
protected void Button2_Click(object sender, EventArgs e)
{
}
//不好意思,因为字数限制,我只好把多余的空格和TAB删掉了
//还希望各位高手帮帮忙,为什么我点删除按钮,没反应 展开
前台:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td align="left">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td align="right">
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Table ID="Table1" runat="server">
</asp:Table>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button2_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
static ArrayList al = new ArrayList();
static int tag = 0;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string target = this.Request["Button1"];
if (target == "添加")
{
if (al.Count > 0)
{
tag++;
}
else
{
tag = 1;
}
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TextBox tb = new TextBox();
Button bt = new Button();
tb.ID = "text" + tag.ToString();
bt.ID = "delete" + tag.ToString();
tr.ID = "row" + tag.ToString();
bt.Text = "删除";
bt.CommandName = tag.ToString();
bt.Command += new CommandEventHandler(delete_Command);
bt.Click += new EventHandler(delete_Click);
tc1.Controls.Add(tb);
tc2.Controls.Add(bt);
tc1.HorizontalAlign = HorizontalAlign.Right;
tc2.HorizontalAlign = HorizontalAlign.Left;
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
al.Add(tr);
}
for (int i = 0; i < al.Count; i++)
{
Table1.Rows.Add((TableRow)al[i]);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void delete_Command(object sender, CommandEventArgs e)
{
for (int i = 0; i < al.Count; i++)
{
string commandname = ((Button)((TableCell)((TableRow)al[i]).Cells[1]).Controls[0]).CommandName;
if (e.CommandName == commandname)
{
al.RemoveAt(i);
break;
}
}
}
protected void delete_Click(object sender, EventArgs e)
{
Response.Write("aaaaaa");
}
protected void Button2_Click(object sender, EventArgs e)
{
}
//不好意思,因为字数限制,我只好把多余的空格和TAB删掉了
//还希望各位高手帮帮忙,为什么我点删除按钮,没反应 展开
1个回答
展开全部
这个 最好就是 你断点调试一下
告诉我们 你的错误在那? 我们再给你解释 !@~
我调式了 一下 发现问题出在 你的按钮 没有触发事件
bt.CommandName = tag.ToString();
bt.Command += new CommandEventHandler(this.delete_Command);
但是 delete_Command事件 从未执行过,
也就是说 你没有添加事件没有成功!~
因为没做过这样的东西 ,但是给你找了个例子 你看看
http://topic.csdn.net/u/20090710/15/28263ffd-e150-4f32-b51a-0876fc72e7fd.html
解决问题后 别忘了 给我发一份!~
告诉我们 你的错误在那? 我们再给你解释 !@~
我调式了 一下 发现问题出在 你的按钮 没有触发事件
bt.CommandName = tag.ToString();
bt.Command += new CommandEventHandler(this.delete_Command);
但是 delete_Command事件 从未执行过,
也就是说 你没有添加事件没有成功!~
因为没做过这样的东西 ,但是给你找了个例子 你看看
http://topic.csdn.net/u/20090710/15/28263ffd-e150-4f32-b51a-0876fc72e7fd.html
解决问题后 别忘了 给我发一份!~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询