开发者

Use repeater to display all the threads in same topic question

开发者 https://www.devze.com 2023-03-30 11:21 出处:网络
I want to use asp.net repeater control to display all threads that under same topic. Each thread has three buttons--reply,edit and delete. Besides buttons, I also want to display author, subject, post

I want to use asp.net repeater control to display all threads that under same topic. Each thread has three buttons--reply,edit and delete. Besides buttons, I also want to display author, subject, post time and content. I have two questions here regarding to how to write button click event and binding data to a div in backend. My front end code is as following:(I use 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&开发者_如何学运维gt;
            <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>

Question 1: how to write button click event in backend, for example I want to use CommandArgument of edit button, what's the click event should be? protected void btn_Edit_Click(object sender,??//I don't know how to write the second)

Question 2: the content stored in database is in html format, What to write in contentDiv_DataBinding event?

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

Thank you in advance!


As the control is nested inside the Repeater, you must refer to it by finding it inside the item being data bound:

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

However, I think it would be better to put a Literal control inside the Div and refer to that instead:

<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");
    }

Regarding controlling the buttons inside the Repeater, you can add a CommandName to the button and call this in the ItemCommand event:

<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"){

         }
    }

Finally, change your Repeater tag to:

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

精彩评论

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