日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

ASP.NET實現(xiàn)Repeater控件的數(shù)據(jù)綁定_基礎(chǔ)知識

作者:大CC ? 更新時間: 2022-09-03 編程語言

Repeater基礎(chǔ)

在aspx文件中加入Repeater 控件,在<ItemTemplate></ItemTemplate>包含的范圍里加入自己控制的代碼,需要替換的變量使用<%# Eval("SellerName")%>;注意兩側(cè)的引號。

.aspx:

<asp:Repeater ID="SellerRpt" runat="server">
    <ItemTemplate>
        <li><a href='<%# Eval("SellerName")%>' target="_blank">
            <%# Eval("ComName")%></a></li>
    </ItemTemplate>
</asp:Repeater>

對應(yīng)的后臺cs中,在頁面加載處加入數(shù)據(jù)綁定的代碼:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = SellerDA.GetTopHotSellers(9);
        SellerRpt.DataSource = dt;
        SellerRpt.DataBind();
    }
}

aspx中"SellerName"、"ComName"為DataTable 中的列名。

優(yōu)化

直接使用DataItem可減少Eval函數(shù)的執(zhí)行步驟,優(yōu)化頁面解析時間:

<%# ((DataRowView)Container.DataItem)["SellerName"]%>替換<%# Eval("SellerName")%>

ArrayList數(shù)據(jù)源

如果數(shù)據(jù)源是ArrayList,并且ArrayList為一列string數(shù)組,則可不用寫出列名:

.aspx:

<asp:Repeater ID="topAdHintRpt" runat="server">
    <ItemTemplate>
        <asp:Label ID="BarLabel" CssClass="bar" runat="server" Text="|"></asp:Label>
        <a href="#"  ><span>
            <%#Container.DataItem%></span></a>
    </ItemTemplate>
</asp:Repeater>

.cs:

ArrayList alterText;
AdDA.GetIndexTopList(out alterText);
topAdHintRpt.DataSource = alterText;
topAdHintRpt.DataBind();

處理后顯示

某些情況下,數(shù)據(jù)庫中檢索出來的數(shù)據(jù)并不適合直接顯示出來,想要適當(dāng)處理后顯示(eg:日期的格式,字符串長度的控制),可使用標(biāo)簽來占位,在onitemdatabound函數(shù)中自行控制:

.aspx:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
    <ItemTemplate>
        <asp:Label ID="colinDate" runat="server" Text=""></asp:Label>
    </ItemTemplate>
</asp:Repeater>

.cs:

protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關(guān)聯(lián)的數(shù)據(jù)項 
        string strDate = rowv["clDate"].ToString();
        Label DateLB = e.Item.FindControl("colinDate") as Label;
        DateLB.Text = strDate.Substring(0, 10);
    }
}

嵌套Reapeter的顯示

對于某些復(fù)雜的顯示邏輯,需用用到Reapeter的嵌套,這里需要自行控制2層數(shù)據(jù)源的數(shù)據(jù)綁定:

.aspx:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
    <ItemTemplate>
        <asp:Repeater ID="ParaRpt" runat="server" OnItemDataBound="ParaRpt_ItemDataBound">
            <ItemTemplate>
                <asp:Label ID="bar" CssClass="bar" runat="server" Text="|"></asp:Label>
                <span class="para">
                    <%# Eval("Name")%>:
                    <%# Eval("Value")%></span>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

.cs:

protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //判斷里層repeater處于外層repeater的哪個位置( AlternatingItemTemplate,F(xiàn)ooterTemplate,
    //HeaderTemplate,,ItemTemplate,SeparatorTemplate
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rep = e.Item.FindControl("ParaRpt") as Repeater;//找到里層的repeater對象
        DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關(guān)聯(lián)的數(shù)據(jù)項 
        string str = Convert.ToString(rowv["Pro_Content"]); //獲取填充子類的內(nèi)容
        rep.DataSource = Product.FillPara(str);
        rep.DataBind();
    }
}

原文鏈接:https://www.cnblogs.com/me115/archive/2011/04/09/2010682.html

欄目分類
最近更新