从“父”转发器获取KeyID

时间:2013-10-31 17:50:09

标签: c# asp.net repeater

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

为了更好地拍照,它是一个博客,每篇文章都有评论,但这些评论在折叠中折叠了。

我可以通过使用OnItemDataBound看到Repeater嵌套在手风琴中,在外部转发器中找到Accordion,使用Accordion.FindControl找到内部转发器。到现在为止还挺好。

然而,正在发生的是所有评论都受到约束,而不仅仅是针对该博客帖子的评论。我需要能够将KeyID传递给内部嵌套的Repeater,以便我可以选择与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";

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

UPDATE - 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>

1 个答案:

答案 0 :(得分: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
    }
}

希望这有帮助。

此致 乌罗什