简体   繁体   English

在中继器中动态创建的LinkBut​​tons不会触发ItemCommand事件

[英]LinkButtons Created Dynamically in a Repeater Don't Fire ItemCommand Event

I've got a repeater that's used to display the output of a dynamic report that takes criteria from webcontrols on the page. 我有一个中继器,用于显示动态报告的输出,该报告从页面上的Web控件获取标准。

Within the repeater's ItemDataBound method I'm adding controls (Linkbuttons for sorting by column values) dynamically to the header of the repeater based on values selected in a checkbox list and at this point setting the CommandArgument and CommandName properties of the linkbuttons. 在转发器的ItemDataBound方法中,我将根据复选框列表中选择的值向转发器的标题动态添加控件(用于按列值排序的链接按钮),并在此时设置链接按钮的CommandArgument和CommandName属性。

The issue is that when the linkbuttons are clicked they don't fire the ItemCommand event although they are clearly being correctly created and added to the header (there is some additional code to set the cssClass, text etc. and this works as expected.) The first column header in the repeater is set in the markup and the itemcommand event fires correctly on this one only. 问题是,当单击链接按钮时,尽管它们显然已正确创建并添加到标题中,但它们不会触发ItemCommand事件(有一些其他代码可以设置cssClass,text等,并且可以按预期工作。)中继器中的第一列标题是在标记中设置的,并且itemcommand事件仅在此对象上正确触发。 When the other column headers are clicked the repeater rebinds as programmed, but the columns are not dynamically re-generated. 单击其他列标题时,转发器将按编程方式重新绑定,但不会动态重新生成这些列。

I would really appreciate somebody explaining what I'm doing wrong - afaik I'm following the approved way of doing this :-( 我真的很感谢有人解释我在做什么错-afaik我正在遵循批准的方式进行此操作:-(

Simplified code follows: 简化的代码如下:

Nightmare.ascx 梦m

<asp:repeater runat="server" id="rptReport" OnItemDataBound="rptResults_ItemDataBound" OnItemCommand="rptResults_ItemCommand" EnableViewState="true">
<headertemplate>
<table>
<tr runat="Server" id="TRDynamicHeader">
<th>
<!-- This one works -->
<asp:Linkbutton runat="server" CommandName="sort" commandArgument='<%# Eval("Name")%?' />
</th>
<!-- additional header cells get added dynamically here -->
</tr>
</headertemplate>
<itemTemplate>
<td><%# Eval("Name")</td>
...
</itemTemplate>

</asp:repeater>

Nightmare.ascx.cs 噩梦.ascx.cs

protected void PageLoad(object sender, eventArgs e){
if (! isPostback){
setupGui();//just binds dropdowns etc. to datasources
}
}

protected void btnRunReport_Click(...){
List<ReportLines> lstRep = GetReportLines();

rptReport.DataSource = lstRep;
repReport.DataBind();
}

protected void rptReport_ItemDataBound (...){
if (e.Item.ItemType == ListItemType.Header)
{
foreach (ListItem li in chbxListBusFuncs.Items)
{
if (li.Selected)               
{
th = new HtmlTableCell();
lb = new LinkButton();
lb.CssClass = "SortColHeader";
lb.CommandArgument = li.Text.Replace(" ", "");
lb.CommandName = "sort";
lb.Text = li.Text;
th.Controls.Add(lb);


((HtmlTableRow)e.Item.FindControl("TRDynamicHeader")).Cells.Add(th);
}
}
}

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Row level customisations, totals calculations etc.

}
}

<!-- this only gets called when the 'hardcoded' linkbutton in the markup is clicked.
protected void rptReport_ItemCommand(object sender, Eventargs e){
lblDebug.Text = string.Format("Well? What's Happening? -> {0}:{1}", e.CommandName, e.CommandArgument.ToString());
}

(The only thing that can call the runreport routine is a single button on the page, not shown in the code snippet above.) (唯一可以调用runreport例程的是页面上的单个按钮,上面的代码段中未显示。)

My guess is that because your dynamically created link buttons are created in the ItemDataBound event, they would not be recreated on postback. 我的猜测是,由于动态创建的链接按钮是在ItemDataBound事件中创建的,因此不会在回发时重新创建它们。 Since it appears the criteria for creating the buttons is not based on repeater data (separate checkbox list), you could probably move the creation of the buttons into an event that will fire on postback as well. 由于创建按钮的条件似乎不是基于中继器数据(单独的复选框列表)的,因此您可以将按钮的创建移动到也会在回发时触发的事件中。

I'm not sure where to suggest you call the button creation code -> Page_Load maybe with some conditional logic? 我不确定在哪里建议您调用按钮创建代码-> Page_Load可能带有一些条件逻辑?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM