您的位置:首页 > 其它

Repeater的复杂使用讲解实例子

2008-04-16 17:59 549 查看
Repeater的复杂使用讲解实例子

在CSDN上看到大家问这到这种贴子,晚上回家想了下大概做下了



做得不完全,给大家一种思想吧(用到的嵌套Repeater控件)有些出入,楼主可自已再改动下,

http://topic.csdn.net/u/20080414/22/b54c735c-b2c0-4225-9a25-080d1309b168.html

代码:




<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Test_GridView8.aspx.cs" Inherits="GridView_Test_GridView8" %>




<!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>


</head>


<body>


<form id="form1" runat="server">


<asp:Repeater runat="server" ID="objRepeater">


<HeaderTemplate>


<table>


<tr>


</tr>


<th>


供应商</th>


<th width="250px">


供应商2</th>


<th>


供应商3</th>


<th>


供应商4</th>


</tr>s


</HeaderTemplate>


<ItemTemplate>


<tr>


<td>


<del>




<%...#Eval("CategoryName") %>


</del>


</td>


<td>


<asp:Repeater DataSource='<%#GetChildren() %>' runat="server">


<ItemTemplate>


<table>


<tr>


<td width="250px">




<%...#Eval("ProductName") %>


</td>


<td width="100px">




<%...#Eval("UnitPrice","{0:c}") %>


</td>


<td width="100px">


<del style="background-color: Red">




<%...#Eval("UnitsInStock","{0:c}") %>


</del>


</td>


</tr>


</table>


</ItemTemplate>


</asp:Repeater>


</td>


</tr>


</ItemTemplate>


<AlternatingItemTemplate>


</AlternatingItemTemplate>


<SeparatorTemplate>


</SeparatorTemplate>


<FooterTemplate>


</table>


</FooterTemplate>


</asp:Repeater>


</form>


</body>


</html>



后代


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;




public partial class GridView_Test_GridView8 : System.Web.UI.Page




...{




变量#region 变量


protected string strFields = "";//字段


protected string strTable = ""; //表名


protected string strWhere = ""; //条件




protected string strSql_Categories = " select CategoryName from Categories where CategoryID = 1"; //执行的SQL


DataSet ds = new DataSet();


#endregion


protected void Page_Load(object sender, EventArgs e)




...{


if (!IsPostBack)




...{


GetData(); //加载显示的数据




}


}


//得到Product表中的数据集


private void GetData()




...{


ds = C_Categories.Instance.GetList_Eds(strSql_Categories);


if (ds.Tables[0].Rows.Count > 0)




...{


this.objRepeater.DataSource = ds.Tables[0];


//objGridView.DataKeyNames = new string[] { "ProductID" };//主键(注意DataKeyNames 是复数间接告诉我们这里是可以有多个字段即可以是联合主键)


this.objRepeater.DataBind();


}


}


public DataSet GetChildren()




...{


string strSql_Products = "select * from products where categoryid in (select categoryid from categories where categoryid=1)";


return C_Products.Instance.GetList_Eds(strSql_Products);


}


}



中间操作类方法:


//常规获得记录


public DataSet GetList_Eds(string strSql)




...{


if (strSql != "")




...{


return DbHelperSql.ExecuteDataSet(strSql.ToString(), null);


}


else




...{


return null;


}


}

数据库链接:


<appSettings>


<add key="ConnectionString" value="Persist Security Info=True;Password=sa;User ID=sa;Initial Catalog=NORTHWIND;Data Source=WANGYONGJUN" />


</appSettings>


<connectionStrings>


<add name="NorthwindConnectionString" connectionString="Persist Security Info=True;Password=sa;User ID=sa;Initial Catalog=NORTHWIND;Data Source=WANGYONGJUN" providerName="System.Data.SqlClient" />


</connectionStrings>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: