使用repeater显示同一主题问题中的所有主题

时间:2011-08-23 21:59:26

标签: c# asp.net repeater

我想使用asp.net转发器控件来显示同一主题下的所有线程。每个线程都有三个按钮 - 回复,编辑和删除。除了按钮,我还想显示作者,主题,发布时间和内容。关于如何编写按钮点击事件并将数据绑定到后端的div,我有两个问题。我的前端代码如下:(我用c#)

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td colspan="2">
                <asp:Button ID="btn_Reply" runat="server" Text="Rreply"     OnClick="btn_Reply_Click" />
                <asp:Button ID="btn_Edit" runat="server" Text="Edit" OnClick="btn_Edit_Click" CommandArgument='<%Eval("id").ToString() %>' />
                <asp:Button ID="btn_Delete" runat="server" Text="Delete" OnClick="btn_Delete_Click" CommandArgument='<%Eval("id").ToString() %>' />
            </td>
       </tr>
       <tr>
            <td align="right" width="100px">Author:</td>
            <td><%#Eval("owner") %></td>
       </tr>
       <tr>
            <td align="right" width="100px">Subject:</td>
            <td><%#Eval("title") %></td>
       </tr>
       <tr>
            <td align="right" width="100px">Post Time:</td>
            <td><%#Eval("timePosted") %></td>
       </tr>
       <tr>
            <td colspan="2">
                <div id="contentDiv" runat="server" ondatabinding="contentDiv_DataBinding"></div>
            </td>
       </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    </asp:Repeater>

问题1:如何在后端编写按钮点击事件,例如我想使用编辑按钮的CommandArgument,点击事件应该是什么? protected void btn_Edit_Click(对象发送者,?? //我不知道怎么写第二个)

问题2:存储在数据库中的内容是html格式,在contentDiv_DataBinding事件中写什么?

 protected void contentDiv_DataBinding(object sender, System.EventArgs e)
    {
         //seems it can not find contentDiv, otherwise, I'll just use contentDiv.html=<%#Eval("content") %>
    }

提前谢谢!

1 个答案:

答案 0 :(得分:1)

由于控件嵌套在Repeater中,您必须通过在数据绑定项中找到它来引用它:

 protected void Repeater_DataBinding(object sender, System.EventArgs e)
    {
         HtmlControl contentDiv = e.item.FindControl("contentDiv");
    }

但是,我认为最好将Literal控件放在Div中并改为引用它:

<div id="contentDiv" runat="server">
   <asp:literal id="ltlContent" runat="server" />
</div>

 protected void Repeater_DataBinding(object sender, System.EventArgs e)
    {
         Literal ltlContent = e.item.FindControl("ltlContent");
         ltlContent = e.Item.DataItem("content");
    }

关于控制Repeater内的按钮,您可以在按钮中添加CommandName并在ItemCommand事件中调用此按钮:

<asp:Button ID="btn_Reply" runat="server" Text="Rreply" CommandName="Reply" CommandArgument='<%Eval("id")%>' />
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%Eval("id").ToString() %>' />
<asp:Button ID="btn_Delete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%Eval("id").ToString() %>' />

 protected void contentDiv_ItemCommand(object sender, System.EventArgs e)
    {
         if (e.CommandName == "Reply"){

         }
         if (e.CommandName == "Edit"){

         }
         if (e.CommandName == "Delete"){

         }
    }

最后,将Repeater标记更改为:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater_DataBinding" OnItemCommand="contentDiv_ItemCommand">