【问题标题】:Fetch Data From Repeater Control On the base of Selected Record基于选定记录从中继器控件中获取数据
【发布时间】:2013-11-22 16:08:43
【问题描述】:

大家好,我的 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

现在基于所选月份我想获取我在该月发布的数据请告诉我我必须在哪个事件上工作

【问题讨论】:

  • 请指定您从中获取数据的表的相关信息
  • @Rony 请检查我编辑我的问题添加我的表
  • @AzadChohan - 使用带有隐藏字段的 ID....

标签: asp.net repeater


【解决方案1】:

使用这种方法。在中继器中放置一个 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
}

【讨论】:

    【解决方案2】:

    您使用两个中继器父母和孩子 在父中继器中,为 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();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多