导航
  • 主页
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm
  • WPF
  • 问答

动态生成控件不响应事件的问题(100分)

jonsonzxw 2003-04-25 10:28:51
我想问一个动态生成控件的问题,在线等,希望尽快答复,谢谢.
在我的网页上有一个Table,它作为我要动态生成的一系列linkbutton控件的容器,在代码中使用循环语句生成了若干linkbutton后,再为它们注册事件处理程序,但当单击这些linkbutton时,就是不执行事件处理程序,怎么办呢?

<table ....>
<tr><td>
....

<asp:Table CellPadding="0" CellSpacing="0" BorderColor="#DDDDDD" BorderStyle="Solid" BorderWidth="0" Runat="server" ID="table">
<asp:TableRow Runat="server"> <asp:TableCell id="Link" Runat="server" />
</asp:TableRow>
</asp:Table>

....
</table>

代码:

//动态生成控件
TableCell cell = (TableCell)table.FindControl("Link");

for(int i=start;i<end;i++)
{
LinkButton lb = new LinkButton();
lb.CommandName = "numbutton";
lb.CommandArgument = i.ToString();
//lb.ID = i.ToString();
lb.Text = i.ToString();
lb.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);
cell.Controls.Add(lb);
}

//事件处理程序
private void OnLinkClick(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
Response.Write("ssssssssssss");
}
...全文
52 点赞 收藏 19
写回复
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonsonzxw 2003-04-25
首先,将我写在BindGrid()方法中的动态产生控件的代码分离出来,单独用一个方法来完成,
其次,在pageload方法的if(!isPostBack)外面,调用刚才的方法,还好,代码写得还比较规范,改起来比较顺利,其实用Command事件或用Click事件都是可以的,再次感谢大家!
回复
jonsonzxw 2003-04-25
感谢众多高手,尤其是timmy3310的指点,我将程序稍微改装了一下,可以了,下面我会将其分享出来,请稍等!
回复
timmy3310 2003-04-25
你的控件要重新创建,否则即使通过__doPostBack回来,也会找不到该控件的实例,更不用说触发它的事件了
回复
jonsonzxw 2003-04-25
加了,但我的这些代码是写在BindGrid()方法中的,如果去掉了ispostback,好象是可以执行到事件代码了,但程序逻辑就有些问题了,我再看看先,非常感谢!!
回复
timmy3310 2003-04-25
你的Page_Load有没有加上

IsPostBack,如果加上是不行的,每次回来控件都需要重新创建
回复
jonsonzxw 2003-04-25
我刚刚调试过,它是运行到每一个 command+=,但没有转到相应的事件处理程序
你的方法我试试先!!
回复
pengweihua 2003-04-25
for(int i=start;i<end;i++)
{
LinkButton lb = new LinkButton();
lb.CommandName = "numbutton";
lb.CommandArgument = i.ToString();
//lb.ID = i.ToString();
lb.Text = i.ToString();
cell.Controls.Add(lb);
}

循環好像不行(個人看法)

try:

先用FindControl找到它
然後再增加事件
回复
jonsonzxw 2003-04-25
高手帮忙呀!!!!!!!!!!!!!
回复
jonsonzxw 2003-04-25
客户端生成的代码:

<table id="table" cellspacing="0" cellpadding="0" bordercolor="#DDDDDD" border="0" style="border-color:#DDDDDD;border-width:0px;border-style:Solid;border-collapse:collapse;">
<tr>
<td nowrap="nowrap"><td id="Link"><span style="color:Red;font-size:9pt;font-weight:bold;">[1]</span> <a href="javascript:__doPostBack('_ctl2','')" style="color:Black;font-size:9pt;">2</a> <a href="javascript:__doPostBack('_ctl4','')" style="color:Black;font-size:9pt;">3</a> <a href="javascript:__doPostBack('_ctl6','')" style="color:Black;font-size:9pt;">4</a> <a href="javascript:__doPostBack('_ctl8','')" style="color:Black;font-size:9pt;">5</a> <a href="javascript:__doPostBack('_ctl10','')" style="color:Black;font-size:9pt;">6</a> <a id="lbafter" href="javascript:__doPostBack('lbafter','')" style="font-size:9pt;">...</a></td>
</tr>
</table>
回复
jonsonzxw 2003-04-25
to pengweihua:

我说了,click我试过,也是与你写的一样,但也不行,并不是语法上有错
回复
jonsonzxw 2003-04-25
客户端生成的代码:

<table id="table" cellspacing="0" cellpadding="0" bordercolor="#DDDDDD" border="0" style="border-color:#DDDDDD;border-width:0px;border-style:Solid;border-collapse:collapse;">
<tr>
<td nowrap="nowrap"><a id="lbtnFirstPage" href="javascript:__doPostBack('lbtnFirstPage','')" style="FONT-FAMILY: webdings">9</a><a id="lbtnPreviousPage" disabled="disabled" style="FONT-FAMILY: webdings">7</a></td><td id="Link"><span style="color:Red;font-size:9pt;font-weight:bold;">[1]</span> <a href="javascript:__doPostBack('_ctl2','')" style="color:Black;font-size:9pt;">2</a> <a href="javascript:__doPostBack('_ctl4','')" style="color:Black;font-size:9pt;">3</a> <a href="javascript:__doPostBack('_ctl6','')" style="color:Black;font-size:9pt;">4</a> <a href="javascript:__doPostBack('_ctl8','')" style="color:Black;font-size:9pt;">5</a> <a href="javascript:__doPostBack('_ctl10','')" style="color:Black;font-size:9pt;">6</a> <a id="lbafter" href="javascript:__doPostBack('lbafter','')" style="font-size:9pt;">...</a></td><td nowrap="nowrap"><a id="lbtnNextPage" href="javascript:__doPostBack('lbtnNextPage','')" style="FONT-FAMILY: webdings">8</a><a id="lbtnLastPage" href="javascript:__doPostBack('lbtnLastPage','')" style="FONT-FAMILY: webdings">:</a></td>
</tr>
</table>
回复
pengweihua 2003-04-25
private void OnLinkClick(object sender, System.EventArgs e)

......



lb.Click += new System.EventHandler(OnLinkClick);
回复
jonsonzxw 2003-04-25
这里有篇文章,我试过了行的,但和我的网页有何分别,为何不灵了??

<%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<%@import namespace="System.Text"%>

<style type="text/css">
TD.ProductDesc
{
font-family: Verdana;
font-size: small;
color: #FF3333;
width: 100%;
vertical-align: top;
}
</style>
<script language="c#" runat="server">

Hashtable hashProductName = new Hashtable();
Hashtable hashProductDesc = new Hashtable();

public void page_load(Object obj,EventArgs e)
{

hashProductName[0] = "Jalapeno Dip" ;
hashProductName[1] = "Smoked Sausage" ;
hashProductName[2] = "Shrimp Fiesta" ;
hashProductName[3] = "Jerk Chicken" ;
hashProductName[4] = "Beer-Battered Fish" ;
hashProductName[5] = "Bacon Burger" ;
hashProductName[6] = "Sirloin Tip" ;
hashProductName[7] = "Baked Alaska" ;
hashProductName[8] = "Fried Chicken" ;
hashProductName[9] = "Fresh Garden Salad" ;
hashProductName[10] = "One Pea" ;

hashProductDesc[0] = "Simmered in mayonaise and wine, this Jalapeno Dip will make your eyes water" ;
hashProductDesc[1] = "Mouth watering and delicious sausage" ;
hashProductDesc[2] = "East Coast's finest shrimp" ;
hashProductDesc[3] = "A real island experience you will not forget" ;
hashProductDesc[4] = "Pabst Blue Ribbon and Fish. Wow!" ;
hashProductDesc[5] = "Big, juicy, and bursting with flavor" ;
hashProductDesc[6] = "Delicate cuts with no fat" ;
hashProductDesc[7] = "Fine dessert comprised of sponge cake topped with ice cream and covered with meringue. " ;
hashProductDesc[8] = "Country cookin'" ;
hashProductDesc[9] = "Crispy iceberg lettuce and a garden of vegtables" ;
hashProductDesc[10] = "A single green pea that will leave you craving more" ;

for (int i=0; i<=10; i++)
{
LinkButton LB5= new LinkButton();
LB5.Text = hashProductName[i].ToString();
LB5.CommandName = "Products";
LB5.CssClass = "ProductLinks";
LB5.CommandArgument = i.ToString() ;
LB5.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);
LinkList.Controls.Add(LB5);
LinkList.Controls.Add(new LiteralControl("<br>"));
}
}

private void OnLinkClick(object O, System.Web.UI.WebControls.CommandEventArgs E)
{
int RecordId = Int32.Parse(E.CommandArgument.ToString());
tablecellMessage.Text="<b>"+hashProductName[RecordId].ToString()+"</b><br><i>"+hashProductDesc[RecordId].ToString()+"</i>";
}
</script>

<form runat="server">

<asp:Table CellPadding=6 CellSpacing=2 BorderColor="#DDDDDD" BorderStyle=Solid BorderWidth=2 Runat=server>
<asp:TableRow Runat=server>
<asp:TableCell id=LinkList Wrap=False BackColor="#FFFFFF" Runat=server/>
<asp:TableCell id="tablecellMessage" CssClass="ProductDesc" Runat=server></asp:TableCell>
</asp:TableRow>
</asp:Table>

</form>
<asp:label id="message" forecolor="red" runat="server"/>
回复
timmy3310 2003-04-25
应该是你的其他部分出了什么问题,你调试看看

然后你到客户端,打开HTML源代码看看,LinkButton后面加了些什么,贴出来看看
回复
jonsonzxw 2003-04-25
我试过click,写法不会有错,但也不会执行,郁闷!!!!
回复
pengweihua 2003-04-25
lb.Click += new System.EventHandler(OnLinkClick);
回复
jonsonzxw 2003-04-25
谁能搞定,给他300分
回复
jonsonzxw 2003-04-25
用Click我也试过也不行,上面我写的方法我以前单独试过是可以的,为何放到实际的项目中就不行了,快疯了!!
回复
visualcpu 2003-04-25
lb.Click+= new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);

看行不行?
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.