开发者

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

开发者 https://www.devze.com 2023-01-02 12:42 出处:网络
I\'ve got a repeater that\'s used to display the output of a dynamic report that takes criteria from webcontrols on the page.

I've got a repeater that's used to display the output of a dynamic report that takes criteria from webcontrols on the page.

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.

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. 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 :-(

Simplified code follows:

Nightmare.ascx

<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 runa开发者_如何学Got="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

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.)


My guess is that because your dynamically created link buttons are created in the ItemDataBound event, they would not be recreated on postback. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消