Repeater数据绑定

Posted 哈喽!树先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Repeater数据绑定相关的知识,希望对你有一定的参考价值。

技术分享

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication1.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 254px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                <ul>
                    <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <li>
                            <%#Eval("name") %>
                            <hr />
                            <asp:HiddenField ID="HiddenField1" Value=‘<%# Eval("id") %>‘ runat="server" />
                            <ul>
                                <asp:Repeater ID="Repeater2" runat="server">
                                <ItemTemplate>
                                    <li>
                                        <asp:LinkButton ID="LinkButton1" CommandArgument=‘<%#Eval("id") %>‘ OnClick="ShowList" runat="server"><%# Eval("Name") %></asp:LinkButton>
                                    </li>
                                </ItemTemplate>
                                </asp:Repeater>  
                            </ul>

                        </li>
                    </ItemTemplate>
                    </asp:Repeater>
                    </ul>
                </td>
                <td>
                    <asp:DataList ID="DataList1" runat="server" RepeatColumns="4">
                        <ItemTemplate>
                            <table class="style1">
                                <tr>
                                    <td>
                                        <asp:Image ID="Image2" runat="server" Height="120px" 
                                            ImageUrl=‘<%# Eval("pic") %>‘ Width="100px" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="Label1" runat="server" Text=‘<%# Eval("name") %>‘></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
          
        }

        private void BindList()
        {
            string sql = "select * from goodClass where pid=0";
            Repeater1.DataSource = SQLHelper.GetTable(sql);
            Repeater1.DataBind();
            foreach ( RepeaterItem item in Repeater1.Items)
            {
                string id = (item.FindControl("HiddenField1") as HiddenField).Value;
                Repeater p2 = item.FindControl("Repeater2") as Repeater;
                string s2 = "select * from goodClass where pid=" + id;
                p2.DataSource = SQLHelper.GetTable(s2);
                p2.DataBind();
            }
        }

        public void ShowList(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            string id = lb.CommandArgument;
            Response.Write("查询类别:" + id + "对应的商品");
            string sql = "select * from goods where cid=" + id;
            DataList1.DataSource = SQLHelper.GetTable(sql);
            DataList1.DataBind();
            
        }
    }
}

 

以上是关于Repeater数据绑定的主要内容,如果未能解决你的问题,请参考以下文章

c#中关于repeater控件的说法

Repeater中 DropDownList如何绑定数据

Repeater数据绑定

通过Repeater控件绑定数据,相同数据合并单元格。

Repeater 绑定的数据怎么获取

我用Repeater绑定数据,我想让Repeater显示的数据 单击其中一个的时候 能自动追加到一个textbox里 怎么实