从Repeater Control获取数据在Selected Record的基础上

时间:2013-11-22 05:38:58

标签: asp.net repeater

Hello Everyone我的asp.net网页上有一个转发器控件。我想从转发器控件中选择月份,从那个月的基数我想得到我在那个月发布的所有数据这里是转发器控件的源代码。

 <asp:Repeater ID="Repeater1" runat="server" 
                onitemcommand="Repeater1_ItemCommand">

            <ItemTemplate>
            <ul class="archive">
            <li><a href="#">
                 <%#Eval("mnth") %> 
            &nbsp; <%#Eval("yr") %><span>(<%#Eval("totalcount") %>)</span>
                <%--<asp:Label ID="Label6" runat="server" Text="<%#Eval("mnth") %>">&nbsp; 
                    <asp:Label ID="Label8" runat="server" Text="<%#Eval("yr") %>"></asp:Label><span> ( 
                    <asp:Label ID="Label7" runat="server" Text="<%#Eval("totalcount") %>"></asp:Label> ) </span> </a></li>--%>
              </ul>
            </ItemTemplate>

            </asp:Repeater>

以下是我用来绑定转发器控件的代码

  private void BindPostCounts()
    {
        SqlCommand cmdBindCounts = new SqlCommand("CountBlogPost_sp", con);
        cmdBindCounts.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter daBindCounts = new SqlDataAdapter(cmdBindCounts);
        DataSet dsBindCount = new DataSet();
        daBindCounts.Fill(dsBindCount);
        Repeater1.DataSource = dsBindCount;
        Repeater1.DataBind();


    }

这是我正在使用的表格

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[BlogPost](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [varchar](500) NULL,
    [Blogpost] [nvarchar](max) NULL,
    [Paramlink] [varchar](500) NULL,
    [PostDate] [datetime] NULL,
    [IsActive] [int] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

现在根据月份选择我想获得我在那个月发布的数据请告诉我我必须工作的事件

2 个答案:

答案 0 :(得分:0)

您使用两个转发器父母和子女 在父转发器中,将方法附加到OnItemDataBound事件,并在方法中查找嵌套转发器并按月绑定数据。

示例(.aspx):

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
    <!-- Repeated data -->
<asp:Label id="monthlabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/>
    <asp:Repeater ID="ChildRepeater" runat="server">
        <ItemTemplate>
            <!-- Nested repeated data -->
        </ItemTemplate>
    </asp:Repeater>
</ItemTemplate>

示例(.cs):

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    ParentRepeater.DataSource = ...;
    ParentRepeater.DataBind();
}}

protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
var Monthname =((Label)e.Item.FindControl("monthlabel")).Text
    Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
    childRepeater.DataSource = ...;
    childRepeater.DataBind();
}

}

答案 1 :(得分:0)

使用此方法。将LinkBut​​ton放在转发器中(示例如下):

<asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate>
        <ul class="archive">
            <li>
                <asp:LinkButton runat="server" Text='<%#Eval("mnth") %>' CommandArgument='<%#Eval("mnth") %>' OnCommand="OnMonthSelected"></asp:LinkButton>
            </li>
        </ul>
    </ItemTemplate>

</asp:Repeater>

然后在OnCommand事件处理程序中获取月份值并按月获取所有博客帖子。

protected void OnMonthSelected(object sender, CommandEventArgs e)
{
    int month = Convert.ToInt32(e.CommandArgument);

    //get blog posts for month
}