Repeater数据绑定-LMLPHP

<%@ 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(); }
}
}
05-08 15:07