简体   繁体   English

如何在C#中的循环中绑定中继器

[英]How to bind repeater in loop in c#

I have used repeater in asp.net 我在asp.net中使用过中继器

 <div class="slider-inner">
<div id="daslider" runat="server" class="da-slider">
<asp:Repeater ID="rptSlider" runat="server">

    <ItemTemplate>
          <asp:Panel ID="sld" runat="server" class="da-slide">
            <h2><asp:Literal ID="lblTitle" runat="server"></asp:Literal></h2>
            <p>
               <asp:Literal ID="lblDescription" runat="server"></asp:Literal>
            </p>
            <div class="da-img">
                 <iframe id="framevid" runat="server" visible="false" width="530" height="300" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
                 <asp:Image ID="sldrimg" runat="server" CssClass="img-responsive"/>
            </div>
        </asp:Panel>
        </ItemTemplate>
     <FooterTemplate>
        <asp:Panel ID="btnlinks" runat="server" class="da-arrows">
            <span class="da-arrows-prev"></span>
            <span class="da-arrows-next"></span>
        </asp:Panel>

    </FooterTemplate>
    </asp:Repeater>
       </div>
       </div>     

In CS File I want to bind them programically using loop as below lines of code 在CS文件中,我想使用循环以编程方式将它们绑定,如下代码行所示

private void GetMainAppSettings()
{ 
    MainSetting Item = context.FetchMainAppSettings();
    SliderContext contextSlider = new SliderContext();
    Slider SW = new Slider();
    string PageName = "Home Page";
    IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
    foreach (Slider item in pType)
    {
        lblTitle.Text = item.SliderTitle;
        lblDescription.Text = item.SliderDescription;
        framevid.Attributes.Add("src", item.SliderImage);
        sldr.Attributes.Add("src", item.SliderImage);
        daslider.Style.Add("background-image", WebUtility.UrlSchemeAuthority() + @"/FileStore/AppSettingsSiteLogos/" + item.BackgroundImage);

 }
 }

Note that GetMainAppSettings() is called on page_load event 请注意,在page_load事件中调用GetMainAppSettings()

Please Help me !!! 请帮我 !!!

No need to loop the data in your code behind, you can directly assign the DataSource and Repeater control will take care of rest. 无需在代码后面循环数据,您可以直接分配DataSource, Repeater控件将负责其余的工作。

In Code behind, you can programatically set the DataSource like this:- 在后面的代码中,您可以通过编程方式设置数据源,如下所示:-

rptSlider.DataSource = pType;
rptSlider.DataBind();

In your repeater control, you can put the Data Binder code nuggets to assign particular properties to control like this:- 在转发器控件中,您可以将Data Binder代码块放置为分配特定属性以进行控制,如下所示:-

<h2><asp:Literal ID="lblTitle" runat="server" Text='<%# SliderTitle%>'></asp:Literal></h2>

and so on..for other controls. 等等..对于其他控件。

There are two separate things that you need to do: 您需要做两件事:

  1. Set the source of the repeater 设置中继器的来源
  2. Tell the repeater what to do for each item in the source. 告诉中继器对源中的每个项目做什么。

To achieve the first, you just need to set the DataSource property of the repeater to the collection of items you need displayed, and execute a DataBind call: 要实现第一个目标,您只需将转发器的DataSource属性设置为您需要显示的项目的集合,然后执行DataBind调用:

private void GetMainAppSettings()
{ 
    MainSetting Item = context.FetchMainAppSettings();
    SliderContext contextSlider = new SliderContext();
    Slider SW = new Slider();
    string PageName = "Home Page";
    IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
    rptSlider.DataSource(pType);
    rptSlider.DataBind();
}

When this is done, the repeater will loop through each item, process it and, display whatever is needed. 完成此操作后,中继器将遍历每个项目,对其进行处理并显示所需的内容。 To customize this process, the repeated provides an ItemDataBound event where you can set how the template should look for a specific item: 为了自定义此过程,重复项提供了ItemDataBound事件,您可以在其中设置模板查找特定项目的方式:

protected void rptSlider_ItemDataBound(object sender, RepeaterItemEventArgs e) {
      // This event is raised for the header, the footer, separators, and items.
      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        //get the item from the event arguments
        var item = (Slider)e.Item.DataItem;
        //get the controls
        var lblTitle = (Label)e.Item.FindControl("lblTitle");
        var lblDescription= (Label)e.Item.FindControl("lblDescription");
        var framevid= (HtmlGenericControl)e.Item.FindControl("framevid");
        var sldr= (HtmlGenericControl)e.Item.FindControl("sldr");
        //set the values
        lblTitle.Text = item.SliderTitle;
        lblDescription.Text = item.SliderDescription;
        framevid.Attributes.Add("src", item.SliderImage);
        sldr.Attributes.Add("src", item.SliderImage);
      }
   } 

This will execute once for each item in the data source, and you have complete control over what goes where and how. 这将对数据源中的每个项目执行一次,并且您可以完全控制什么位置以及如何进行。 The looping is done implicitly for you by the repeater itself. 中继器本身将为您隐式完成循环。

Why use loop for bind repeater? 为什么要对绑定转发器使用循环? You can directly assign your object "pType" to repeater data source. 您可以直接将对象“ pType”分配给中继器数据源。 Like 喜欢

 IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
 rptSlider.DataSource=pType;
 rptSlider.DataBind();

After you can access all your field in repeater on .aspx page. 之后,您可以在.aspx页上访问转发器中的所有字段。

More Details see below article: http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-use-repeater-control-in-Asp-Net/ 更多详细信息,请参阅以下文章: http : //www.c-sharpcorner.com/UploadFile/5089e0/how-to-use-repeater-control-in-Asp-Net/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM