注册 登录
编程论坛 ASP.NET技术论坛

[求助]关于动态生成LinkButton控件的command事件

tkgg888 发布于 2007-05-28 16:06, 3694 次点击

想要在表格里动态生成几行web控件,其中最后一列为需要添加事件的按钮。
我用了循环来实现这个功能代码如下:
page_load中:
for(int j=0;j<arryse1.Count/4;j++)
{
HtmlTableRow row=new HtmlTableRow();
for(int i=0;i<4;i++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","xx-small");
cell.Align="center";
cell.InnerText="华文新魏"+j.ToString();
row.Cells.Add(cell);
}
HtmlTableCell cell1=new HtmlTableCell();
cell1.InnerText="15";
row.Cells.Add(cell1);
HtmlTableCell cell2=new HtmlTableCell();
LinkButton delbutton=new LinkButton();
delbutton.Text="删除";
delbutton.CommandName=j.ToString ();
//Response.Write("<script>alert('"+j.ToString()+"');</script>");
delbutton.Command+=new CommandEventHandler(delbutton_Command);
cell2.Controls.Add(delbutton);
row.Cells.Add(cell2);
this.Table1.Rows.Insert(this.Table1.Rows.Count-1,row);
}
delbutton_command中:
ArrayList arryse=new ArrayList();
arryse=(ArrayList)Session["ar"];
if(arryse.Count>4)
{

for(int p=1;p<=arryse.Count/4;p++)
{
this.Table1.Rows.RemoveAt(1);
}
arryse.RemoveRange(Convert.ToInt32(e.CommandName)*4,4);
Session["ar"]=arryse;
for(int j=0;j<arryse.Count/4;j++)
{
HtmlTableRow row=new HtmlTableRow();
for(int i=0;i<4;i++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","xx-small");
cell.InnerText=arryse[4*j+i].ToString();
cell.Align="center";
row.Cells.Add(cell);
}
HtmlTableCell cell1=new HtmlTableCell();
cell1.InnerText="15";
row.Cells.Add(cell1);
HtmlTableCell cell2=new HtmlTableCell();
LinkButton delbutton=new LinkButton();
delbutton.Text="删除";
cell2.Controls.Add(delbutton);
row.Cells.Add(cell2);
this.Table1.Rows.Insert(this.Table1.Rows.Count-1,row);
}
}
else
{
this.Table1.Rows.RemoveAt(1);
arryse.RemoveRange(0,4);
}
出现的情况为:
点击第一行生成的按钮会执行delbutton_command事件,可点击其它行的按钮时却不执行此事件。
这是怎么一回事!!请教!急!


19 回复
#2
cyyu_ryh2007-05-28 16:45

应该每次就要执行

#3
tkgg8882007-05-28 17:03
我也是这想得,可实际上只有第一行的控件执行点击事件,其它的都不执行
#4
tkgg8882007-05-28 17:50
大家帮看看呀!
倒底是哪儿出了问题了,困扰了我好长时间了!!
#5
tkgg8882007-05-29 11:20

为什么没人理我呀!!
再解决不了这个问题,我就得下岗了!!

#6
GrimFish2007-05-29 11:24
代码全部放出来吧,你就给这么一点点,总不可能别人帮你测试的时候还要去写代码?包括.aspx的
#7
tkgg8882007-05-29 11:44
放出来一大堆,谁愿意看呀!
#8
tkgg8882007-05-29 11:48

这是aspx的
<body>
<form id="Form1" style="TABLE-LAYOUT: fixed" method="post" runat="server">
<asp:panel id="Panel1" runat="server" HorizontalAlign="Center" Height="16px">
<asp:Label id="Label2" runat="server" Font-Size="X-Large" Font-Bold="True">用户进货</asp:Label>
</asp:panel><asp:panel id="Panel2" runat="server" HorizontalAlign="Center" Width="785px">
<P></P>
<FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体">
</FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT>
<TABLE id="Table3" style="WIDTH: 781px; HEIGHT: 192px" cellSpacing="1" cellPadding="1"
width="781" border="1">
<TR>
<TD style="WIDTH: 173px" align="center">品名</TD>
<TD style="WIDTH: 152px">
<asp:DropDownList id="DropDownList2" runat="server" Width="174px"></asp:DropDownList></TD>
<TD style="WIDTH: 63px" align="center">数量</TD>
<TD>
<asp:TextBox id="TextBox2" runat="server" Width="46px"></asp:TextBox></TD>
<TD style="WIDTH: 110px" align="center">单价(元)</TD>
<TD style="WIDTH: 62px"><FONT face="宋体">
<asp:Label id="Label3" runat="server" Font-Size="X-Small" Width="83px">Label</asp:Label></FONT></TD>
<TD style="WIDTH: 52px" align="center">单位</TD>
<TD>
<asp:DropDownList id="DropDownList3" runat="server" Width="74px"></asp:DropDownList></TD>
<TD>
<asp:Button id="Button1" runat="server" Width="58px" Text="添加"></asp:Button></TD>
</TR>
<TR>
<TD vAlign="top" colSpan="9">
<TABLE id="Table1" style="TABLE-LAYOUT: fixed; WIDTH: 772px; HEIGHT: 150px" cellSpacing="1"
cellPadding="1" width="772" align="center" border="1" runat="server">
<TR>
<TD style="WIDTH: 175px" align="center"><FONT face="宋体">产品名称</FONT></TD>
<TD style="WIDTH: 87px" align="center"><FONT face="宋体">数量</FONT></TD>
<TD style="WIDTH: 129px" align="center"><FONT face="宋体">单价(元)</FONT></TD>
<TD style="WIDTH: 125px" align="center"><FONT face="宋体">单位</FONT></TD>
<TD style="WIDTH: 159px" align="center"><FONT face="宋体">总价(元)</FONT></TD>
<TD align="center"><FONT face="宋体">删除</FONT></TD>
</TR>
<TR>
<TD vAlign="top" colSpan="6"><FONT face="宋体">
<TABLE id="Table2" style="TABLE-LAYOUT: fixed; WIDTH: 766px; HEIGHT: 99px" cellSpacing="1"
cellPadding="1" width="766" border="0">
<TR>
<TD style="WIDTH: 121px">合计</TD>
<TD colSpan="6">
<asp:Label id="Label1" runat="server" Width="456px">Label</asp:Label></TD>
</TR>
<TR>
<TD style="WIDTH: 121px">备注</TD>
<TD colSpan="6">
<asp:TextBox id="TextBox1" runat="server" Width="640px"></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 121px">运输方式:</TD>
<TD colSpan="6">
<asp:DropDownList id="DropDownList1" runat="server" Width="160px"></asp:DropDownList></TD>
</TR>
<TR>
<TD style="WIDTH: 121px">收货地址:</TD>
<TD style="WIDTH: 273px">
<asp:TextBox id="TextBox3" runat="server" Width="252px"></asp:TextBox></TD>
<TD style="WIDTH: 141px">收货人:</TD>
<TD style="WIDTH: 67px">
<asp:TextBox id="TextBox4" runat="server" Width="71px"></asp:TextBox></TD>
<TD style="WIDTH: 67px">电话:</TD>
<TD colSpan="2">
<asp:TextBox id="TextBox5" runat="server"></asp:TextBox></TD>
</TR>
</TABLE>
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<asp:Button id="Button3" runat="server" Width="65px" Text="提 交"></asp:Button>
<FONT face="宋体">&nbsp; </FONT>
<asp:Button id="Button4" runat="server" Width="59px" Text="重 置"></asp:Button>
下面是代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace lkjxc
{
/// <summary>
/// custommain 的摘要说明。
/// </summary>
public class custommain : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.DropDownList DropDownList4;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.DropDownList DropDownList5;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Panel Panel4;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.TextBox TextBox7;
protected System.Web.UI.WebControls.TextBox TextBox8;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.WebControls.Button Button4;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ArrayList arryse=new ArrayList();
ArrayList arryse1=(ArrayList)Session["ar"];
if(Page.IsPostBack==true)
{
if(Session["id"].ToString()=="1" && Session["ar"]!=null)
{
ArrayList hwqdbefor=(ArrayList)Session["ar"];
Session["id"]="2";
for(int j=0;j<arryse1.Count/4;j++)
{
HtmlTableRow row=new HtmlTableRow();
for(int i=0;i<4;i++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","xx-small");
cell.Align="center";
cell.InnerText="华文新魏"+j.ToString();
row.Cells.Add(cell);
}
HtmlTableCell cell1=new HtmlTableCell();
cell1.InnerText="15";
row.Cells.Add(cell1);
HtmlTableCell cell2=new HtmlTableCell();
LinkButton delbutton=new LinkButton();
delbutton.Text="删除";
delbutton.CommandName=j.ToString ();
//Response.Write("<script>alert('"+j.ToString()+"');</script>");
delbutton.Command+=new CommandEventHandler(delbutton_Command);
cell2.Controls.Add(delbutton);
row.Cells.Add(cell2);
this.Table1.Rows.Insert(this.Table1.Rows.Count-1,row);
}
}
}
switch(Request["custom"].ToString())
{
case "用户进货":
this.Label2.Text="用户进货";
this.Panel2.Visible=true;
this.Panel3.Visible=false;
this.Panel4.Visible=false;
break;
case "进货历史":
this.Label2.Text="进货历史";
this.Panel3.Visible=true;
this.Panel2.Visible=false;
this.Panel4.Visible=false;
break;
case "密码维护":
this.Label2.Text="密码维护";
this.Panel4.Visible=true;
this.Panel3.Visible=false;
this.Panel2.Visible=false;
break;

}

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList arryse=new ArrayList();
if(Session["ar"]!=null)
{

ArrayList hwqdbefor=(ArrayList)Session["ar"];
if(Session["id"].ToString()=="2")
{
for(int p=1;p<=hwqdbefor.Count/4;p++)
{
this.Table1.Rows.RemoveAt(1);
}
}
string[] hwqd=new string[hwqdbefor.Count+4];
hwqd[0]=this.DropDownList2.SelectedValue.ToString();
hwqd[1]=this.TextBox2.Text;
hwqd[2]=this.Label3.Text;
hwqd[3]=this.DropDownList3.SelectedValue.ToString();
for(int k=4;k<hwqdbefor.Count;k++)
{
hwqd[k]=hwqdbefor[k-4].ToString();
}
for(int i=0;i<hwqd.Length;i++)
{
arryse.Add(hwqd[i]);
}
}
else
{
string[] hwqd=new string[4];
hwqd[0]=this.DropDownList2.SelectedValue.ToString();
hwqd[1]=this.TextBox2.Text;
hwqd[2]=this.Label3.Text;
hwqd[3]=this.DropDownList3.SelectedValue.ToString();
for(int i=0;i<4;i++)
{
arryse.Add(hwqd[i]);
}
}
Session.Add("ar",arryse);
ArrayList arryse1=(ArrayList)Session["ar"];
Session.Add("id","1");
for(int j=0;j<arryse1.Count/4;j++)
{
HtmlTableRow row=new HtmlTableRow();
for(int i=0;i<4;i++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","xx-small");
cell.Align="center";
cell.InnerText=j.ToString();
row.Cells.Add(cell);

}
HtmlTableCell cell1=new HtmlTableCell();
cell1.InnerText="15";
row.Cells.Add(cell1);
HtmlTableCell cell2=new HtmlTableCell();
LinkButton delbutton=new LinkButton();
delbutton.Text="删除";
cell2.Controls.Add(delbutton);
row.Cells.Add(cell2);
this.Table1.Rows.Insert(this.Table1.Rows.Count-1,row);
}
}

private void delbutton_Command(object sender, CommandEventArgs e)
{
ArrayList arryse=new ArrayList();
arryse=(ArrayList)Session["ar"];
Response.Write("<script>alert('"+Session["id"].ToString()+"');</script>");
if(arryse.Count>4)
{

for(int p=1;p<=arryse.Count/4;p++)
{
this.Table1.Rows.RemoveAt(1);
}
arryse.RemoveRange(Convert.ToInt32(e.CommandName)*4,4);
Session["ar"]=arryse;
for(int j=0;j<arryse.Count/4;j++)
{
HtmlTableRow row=new HtmlTableRow();
for(int i=0;i<4;i++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","xx-small");
cell.InnerText=arryse[4*j+i].ToString();
cell.Align="center";
row.Cells.Add(cell);
}
HtmlTableCell cell1=new HtmlTableCell();
cell1.InnerText="15";
row.Cells.Add(cell1);
HtmlTableCell cell2=new HtmlTableCell();
LinkButton delbutton=new LinkButton();
delbutton.Text="删除";
cell2.Controls.Add(delbutton);
row.Cells.Add(cell2);
this.Table1.Rows.Insert(this.Table1.Rows.Count-1,row);
}
Session["id"]="1";

}
else
{
Session["id"]="0";
this.Table1.Rows.RemoveAt(1);
arryse.RemoveRange(0,4);
Response.Write("<script>alert('"+Session["id"].ToString()+"');</script>");
}

}
}
}
以上就是全部内容

#9
GrimFish2007-05-29 12:24

感觉你这个是个购物车的程序。你可以参考相关例子,不好意思,实在没法调试,你还有很多用户控件没放上来。参照网上的购物车程序吧。

#10
tkgg8882007-05-29 14:26
和那个差不多,但我在这儿不想用数据库!!
#11
GrimFish2007-05-29 14:34

购物车程序一般都不使用数据库,而是用SESSION或者用COOKIE来做临时储存,网上很多例子,LZ可以去看看参考。
包括MS官方都有这方面的例子

#12
tkgg8882007-05-30 11:02

我现在需要的不是怎么做购物车,只是需解决,那个动态生成的删除按钮的事件问题。
为什么用循环生成再第一次循环生成的时候可以执行,而在后几次循环中却不执行了!!

#13
tkgg8882007-05-30 14:02
还没人理么??
#14
凌枫影2007-05-30 14:29

你給生成的  delbutton  設一個ID看一下

#15
川流不息2007-05-30 14:38
不好意思哈,我想問一下:
你動態生成的LinkButton控件所調用的事件是不是同一個事件:delbutton_Command,不用傳參數?
如果是的話,我先不管它為什麼只有第一個能觸發這個事件了,我沒有研究過,我建議這樣處理:
作一個隱藏按鈕,把delbutton_Command這個事件裡面的內容寫在按鈕的點擊事件裡面,然後,你在動態生成的LinkButton上添加JS事件,在點擊動態生成的LinkButton控件時觸發的JS事件裡面去調用那個隱藏按鈕的點擊事件。
即:在JS事件中寫:document.all.btnHid.click();

當然,這是個人建議,你不妨考慮一下。
#16
tkgg8882007-05-30 15:02
不管行不行,先谢过大家了!!我去试试
#17
tkgg8882007-05-30 16:21
还是不行。现在搞不清楚问题的原因,即使以后用到类似的问题也是个麻烦事呀!
#18
川流不息2007-05-30 16:24
不行嗎?那麼,你的JS事件,點擊的時候,是只是第一個觸發了還是都可以觸發?
#19
tkgg8882007-05-30 16:52
一个也没有
#20
zidu2007-05-30 17:28
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
LinkButton link = new LinkButton();
link.Text = "Click"+i.ToString();
link.CommandName = i.ToString();
link.Command += new CommandEventHandler(click_command);
test1.Controls.Add(link);
}
}
void click_command(object sender, CommandEventArgs e)
{
msg.Text = "This is Link " + e.CommandName;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div id="test1" runat="server">
</div>
<asp:Literal ID="msg" runat="server"></asp:Literal>
</form>
</body>
</html>

执行-->查看源文件-->
<div id="test1">    
<a href="javascript:__doPostBack('ctl02','')">Click0</a>
<a href="javascript:__doPostBack('ctl03','')">Click1</a>
<a href="javascript:__doPostBack('ctl04','')">Click2</a>
<a href="javascript:__doPostBack('ctl05','')">Click3</a></div>



1