开发者

Add controls dynamically to a listview

I have an asp.net listview. The data that populates it is sorted into groups, and i'm using some code in the listview item template, that essentially checks if the grouping field has changed in the data, and if so it prints a new row, with the heading, so it looks a bit like this:

<ItemTemplate>
<%# AddGroupingRowIfWPHasChanged() %开发者_JAVA技巧>

    <tr id="row" class="GridViewRow" runat="server" >
    <td valign="top" align="left"  ><%# Eval("item1") %></td>
.......
</itemTemplate>

This works great. However now I need to add a button the the group heading field, and I can't seem to get this to work. Obvioulsy this button only needs adding if the heading has changed, but the AddGroupingRowIfWPHasChanged() method does not seem to have access to the current ListviewItem, so I can't add a control.

Any suggestions on how I can add a control to a list view dynamically, depandant on whether its a group heading or not?


It may be easier to override the ItemInserting event in the codebehind and do it from there


Use a placeholder?

.aspx

<asp:Placeholder id="plhControlHolder" runat="server" />

code-behind

' ** use FindControl if instead a databound event, otherwise you could skip.
Dim plcControlHolder as PlaceHolder = e.Row.FindControl("plcControlHolder") 
Dim btnDynamic As New Button
btnDynamic.Id = "MyButton"
plcControlHolder.Controls.Add(btnDynamic)


Try using the GroupTemplate and GroupSeparatorTemplate instead of trying to fit it all in the ItemTemplate.


Create a UserControl to include in place of the AddGroupingRowIfWPHasChanged() method call. Override Render(). Use the logic that's currently in AddGroupingRowIfWPHasChanged() to render the control if the group has changed; otherwise, render an empty string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜