| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3691 人关注过本帖
标题:[求助]关于动态生成LinkButton控件的command事件
只看楼主 加入收藏
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
 问题点数:0 回复次数:19 
[求助]关于动态生成LinkButton控件的command事件

想要在表格里动态生成几行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事件,可点击其它行的按钮时却不执行此事件。
这是怎么一回事!!请教!急!


搜索更多相关主题的帖子: command 控件 LinkButton web 动态 
2007-05-28 16:06
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 

应该每次就要执行


有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-05-28 16:45
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 
我也是这想得,可实际上只有第一行的控件执行点击事件,其它的都不执行

2007-05-28 17:03
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 
大家帮看看呀!
倒底是哪儿出了问题了,困扰了我好长时间了!!

2007-05-28 17:50
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 

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


2007-05-29 11:20
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
收藏
得分:0 
代码全部放出来吧,你就给这么一点点,总不可能别人帮你测试的时候还要去写代码?包括.aspx的
2007-05-29 11:24
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 
放出来一大堆,谁愿意看呀!

2007-05-29 11:44
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 

这是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>");
}

}
}
}
以上就是全部内容


2007-05-29 11:48
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
收藏
得分:0 

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

2007-05-29 12:24
tkgg888
Rank: 1
等 级:新手上路
帖 子:341
专家分:2
注 册:2006-4-24
收藏
得分:0 
和那个差不多,但我在这儿不想用数据库!!

2007-05-29 14:26
快速回复:[求助]关于动态生成LinkButton控件的command事件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044941 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved