作者:Mr.XQIJIANG
出处: http://www.cnblogs.com/Mr-XQIJIANG/本文版权归【Mr.XQIJIANG】所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
数据库:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 500px;
border-left-style: solid;
border-left-width: 1px;
border-right: 1px solid #C0C0C0;
border-top-style: solid;
border-top-width: 1px;
border-bottom: 1px solid #C0C0C0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" cellpadding="0" cellspacing="3" class="style1">
<tr>
<td align="center">
自动编号</td>
<td align="center">
效果展示</td>
</tr>
<asp:Repeater ID="repeater1" runat="server">
<ItemTemplate>
<tr>
<td align="center">
<%#Eval("Qid") %></td>
<td align="center">
<%# Convert.ToString(Eval("Hname.Hname")).Replace("*", Convert.ToString(Eval("Qname")))%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Show();
}
}
private void Show()
{
bllQinfo bll = new bllQinfo();
List<modQinfo> mods = bll.GetAll();
this.repeater1.DataSource = mods;
this.repeater1.DataBind();
}
三层代码:
-----------------表1----------------------
public class modQinfo
{
private int _qid;
public int Qid
{
get { return _qid; }
set { _qid = value; }
}
private int _qname;
public int Qname
{
get { return _qname; }
set { _qname = value; }
}
private int _hid;
public int Hid
{
get { return _hid; }
set { _hid = value; }
}
private modHhtml hname;
public modHhtml Hname
{
get { return hname; }
set { hname = value; }
}
}
---------------表2-----------------
public class modHhtml
{
private int _hid;
public int Hid
{
get { return _hid; }
set { _hid = value; }
}
private string _hname;
public string Hname
{
get { return _hname; }
set { _hname = value; }
}
}
DAL数据访问层:
---------表1------------
public List<modQinfo> GetAll()
{
string sql = "select * from Qinfo";
DBHelp db = new DBHelp();
DataSet objset = db.GetDataSet(sql);
dalHinfo dal = new dalHinfo();
List<modQinfo> mods = new List<modQinfo>();
foreach (DataRow dr in objset.Tables[0].Rows)
{
modQinfo mod = new modQinfo();
mod.Qid = Int32.Parse(dr["Qid"].ToString());
mod.Qname = Int32.Parse(dr["Qname"].ToString());
mod.Hid = Int32.Parse(dr["Hid"].ToString());
mod.Hname = dal.getHid(mod.Hid);
mods.Add(mod);
}
return mods;
}
-------------表2---------------
public modHhtml getHid(int hid)
{
string sql = "select * from Hhtml where hid=" + hid;
modHhtml mod = new modHhtml();
DBHelp db = new DBHelp();
DataSet objset = db.GetDataSet(sql);
foreach (DataRow dr in objset.Tables[0].Rows)
{
mod.Hid = Int32.Parse(dr["Hid"].ToString());
mod.Hname = dr["Hname"].ToString();
}
return mod;
}
-----------表1-------------
dalQinfo dal = new dalQinfo();
public List<modQinfo> GetAll()
{
return dal.GetAll();
}
----------表2-------------
dalHinfo dal = new dalHinfo();
public modHhtml getHid(int hid)
{
return dal.getHid(hid);
}