0

我有一个中继器 - 在 Ajax 手风琴中 - 在外部中继器的 ItemTemplate 中。

为了更好地描绘它,它是一个博客,每个帖子下都有评论,但这些评论在手风琴中折叠。

我可以通过使用 OnItemDataBound 查看嵌套在手风琴内部的中继器,在外部中继器中查找 Accordion,并通过使用 Accordion.FindControl 查找内部中继器。到目前为止,一切都很好。

然而,正在发生的事情是所有评论都被绑定,而不仅仅是特定于该博客文章的评论。我需要能够将 KeyID 传递给内部嵌套的中继器,以便我可以选择与 ID = KeyID 的每个博客文章相关的评论。

我猜测 ID 会以某种方式与 RepeaterItemEventArgs 相关联,但我不知道这是如何发生的,也不知道如何确定我正在使用的博客 KeyID。

    protected void BindComments(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            AjaxControlToolkit.Accordion myCommentsAccordion = (AjaxControlToolkit.Accordion)e.Item.FindControl("CommentsAccordion");
            Repeater myCommentRepeater = (Repeater)myCommentsAccordion.FindControl("CommentsRepeater");

            Utility myUtility = new Utility();
            SqlConnection myConn = myUtility.GetConnection();
            string myCommandText = "select [CommentID],[ShadeID],[Commenter],[CommentDate],[Comment] from [Comments] where ShadeID = " + ???? + " order by CommentDate DESC";

如果更多信息会有所帮助,请告诉我。我不确定什么是有用的,什么只是使问题变得混乱。

更新 - aspx 如下要求:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="TestShades.aspx.cs" Inherits="DisciplesWalk.TestShades" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="Content" runat="server">
<asp:Image ImageAlign= "Middle" ID="ShadesBanner" runat="server" ImageUrl="~/Images/Banners/Shades.jpg" />
<asp:Label ID="myErrorLabel" runat="Server"  CssClass="failureNotification"></asp:Label>
    <div id="ShadesDiv" class="ShadesDiv">
    <asp:Repeater ID="ShadesRepeater" runat="server" OnItemDataBound="BindComments">
        <ItemTemplate>
            <!-- top level repeater element template here -->
            <br />
            <asp:Label ID="ShadeDate" CssClass="ShadeDate" runat="server" Width="100%" Text='<%# Bind("ShadeDate", "{0:MM/dd/yyyy}") %>'></asp:Label>
            <span class="notehead">
            <asp:Label ID="ShadeTitle" CssClass="ShadeHeading" runat="server" Width="100%" Text='<%# Bind("ShadeTitle")%>'></asp:Label>
            </span>
            <br /><br />
            <span>
            <img alt="" src="Images/shades/<%# Eval("ShadeImage") %>"  style = "width:200px; padding-right:9px; padding-top:0px; padding-bottom:3px; float:left;" />
            <%# Eval("ShadeBody") %>
            </span>
            <br />
            <AjaxToolkit:Accordion ID="CommentsAccordion" runat="server"
            SelectedIndex="-1"
            CssClass="accordion"
            HeaderCssClass="accordionHeader"
            HeaderSelectedCssClass="accordionHeaderSelected"
            ContentCssClass="accordionContent"
            AutoSize="None"
            FadeTransitions="true"
            TransitionDuration="250"
            FramesPerSecond="40"
            RequireOpenedPane="false"
            SuppressHeaderPostbacks="true">
            <Panes>
                <AjaxToolkit:AccordionPane runat="server" ID="OverviewPane">
                    <Header>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Comments</Header>
                    <Content>
                        <div style="overflow-y:scroll;height:450px"> 
                            <asp:Repeater ID="CommentsRepeater" runat="server">
                                <ItemTemplate>
                                    <br />
                                    <span>
                                    <asp:Label ID="CommenterLabel" runat="server" Width="100%" Text='<%# Eval("Commenter")%>'></asp:Label>&nbsp;&nbsp;
                                    <asp:Label ID="Label1" runat="server" Width="100%" Text='<%# Eval("CommentDate")%>'></asp:Label>
                                    </span>
                                    <span>
                                    <asp:Label ID="CommentBody" runat="server" Width="100%" Text='<%# Eval("Comment")%>'></asp:Label>
                                    </span>
                                    <br />
                                    <hr class="HRSeperator" />
                                    <br /><br />
                                </ItemTemplate> 
                            </asp:Repeater>
                        </div>
                    </Content>
                </AjaxToolkit:AccordionPane>
            </Panes>
        </AjaxToolkit:Accordion>
        </ItemTemplate>
        <SeparatorTemplate>
            <br />
            <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/separator.jpg" ImageAlign="Middle" />
            <br /><br />
        </SeparatorTemplate> 
    </asp:Repeater>
    </div>
    <asp:Table ID="AddCommentTable" runat="server" CellPadding="12" cellspacing="0" width="100%" Visible="false">
        <asp:TableRow>
            <asp:TableCell  HorizontalAlign="Left">
                <br />
                <asp:Label runat="server" ID="AddCommenterLabel" Text="Name:" Visible="True"></asp:Label>
                <br />
                <asp:TextBox ID="CommenterTextBox" runat="server" Width="25%"></asp:TextBox>
                <br />
                <asp:Label runat="server" ID="CommentLabel" Text="Your comment:" Visible="True"></asp:Label>
                <br />
                <asp:TextBox ID="CommentTextBox" runat="server" Width="90%"></asp:TextBox>
                <br /><br />
                <asp:Button ID="AddCommentButton" runat="server" Text="Submit Comment" onclick="AddCommentButton_Click" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</asp:Content>
4

1 回答 1

1

我认为你可以这样做:

在.aspx文件上面定义

<AjaxToolkit:Accordion ID="CommentsAccordion" runat="server" 

把这条线

<asp:Label ID="lblShadeID" runat="server" Text='<%# Eval("ShadeID") %>' Visible="false"></asp:Label>

然后将此代码用于BindComments方法:

protected void BindComments(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        AjaxControlToolkit.Accordion myCommentsAccordion = (AjaxControlToolkit.Accordion)e.Item.FindControl("CommentsAccordion");
        Repeater myCommentRepeater = (Repeater)myCommentsAccordion.FindControl("CommentsRepeater");

        var label = e.Item.FindControl("lblShadeID") as Label;
        int shadeId = Convert.ToInt32(label.Text);

        Utility myUtility = new Utility();
        SqlConnection myConn = myUtility.GetConnection();
        string myCommandText = "select [CommentID],[ShadeID],[Commenter],[CommentDate],[Comment] from [Comments] where ShadeID = @shadeId order by CommentDate DESC";
        // set command parameter named @shadeId to the value of shadeId, execute the query and bind data to myCommentRepeater
    }
}

希望这可以帮助。

问候,乌罗斯

于 2013-10-31T20:32:51.987 回答