EF的高级使用##
- 1.添加一个WebForm:WebForm1.aspx
- 2.使用Repeater加载数据↓
<div>
<%--隐藏控件--%>
<asp:HiddenField ID="HiddenField1" runat="server" />
<p>标题:<asp:TextBox ID="tide" runat="server"></asp:TextBox></p>
<p>作者:<asp:TextBox ID="Author" runat="server"></asp:TextBox></p>
<p>内容:<asp:TextBox ID="Content" runat="server"></asp:TextBox></p>
<p>标签:<asp:TextBox ID="CatelogName" runat="server"></asp:TextBox></p>
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
<table border="1">
<tr>
<th>标题</th>
<th>作者</th>
<th>内容</th>
<th>标签</th>
<th>操作</th>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td><%# Eval("tide")%></td>
<td><%# Eval("author")%></td>
<td><%# Eval("content")%></td>
<td><%# Eval("catelogname")%></td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="update" CommandArgument='<%# Eval("id")%>'>编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" CommandArgument='<%# Eval("id")%>'>删除</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
页面顶端通过Button1点击事件进行添加
Repeater底部使用LinkButton进行编辑和删除
具体代码如下↓
//创建一个EF数据上下文对象
MyBKEntities1 My = new MyBKEntities1();
使用linq查询数据上下文Article表数据
挑选出自己要的数据字段,在用匿名对象存放
var art = from article in My.Article
join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
select new
{
id = article.Id,
tide = article.tide,
author = article.Auathor,
content = article.Content,
catelogname = catelog.Name
};
将查询出的数据绑定到Repeater1控件中
this.Repeater1.DataSource = art.ToList();
this.Repeater1.DataBind();
编写为一个方法
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sele();
}
}
将要添加的数据,封装成对象,在返回回来
public Article arti()
{
//使用linq查询数据上下文Catelog表的id
var cate = from catelog in My.Catelog
//用输入的标题作为条件
where catelog.Name == this.CatelogName.Text
select new { id = catelog.Id };
//实例化一个Article对象并且赋值
Article article = new Article();
article.tide = this.tide.Text;
article.Auathor = this.Author.Text;
article.Content = this.Content.Text;
article.time = DateTime.Now;
try
{
//在这没有报错说明数据库输入的标签在数据库是存在的,直接赋值
article.Catelogid = cate.ToList()[0].id;
}
catch (Exception)
{
//在上面报错说明输入的标签在数据库中不存在,就要先添加在赋值
Catelog catelog = new Catelog() { Name = this.CatelogName.Text };//实例化一个Catelog对象并且赋值
My.Catelog.Add(catelog);
//调用数据上下文中Add方法将Catelog对象添加到数据库中,这里对对象的操作,实际上是对代理类的操作
//SaveChanges真实的保存到数据库
My.SaveChanges();
//使用linq查询数据上下文Catelog表的id
var cates = from catelogs in My.Catelog
where catelogs.Name == this.CatelogName.Text
select new { id = catelogs.Id };
article.Catelogid = cates.ToList()[0].id;//将输入的标签对应的id赋值给article对象中的Catelogid字段
}
//最后返回一个对象
return article;
}
protected void Button1_Click(object sender, EventArgs e)
{
//添加和编辑都是Button1控件所以判断一下
if (this.Button1.Text == "添加")
{
var article = arti();//调用将要添加的数据,封装成对象方法
My.Article.Add(article);
//调用数据上下文中Add方法将Catelog对象添加到数据库中,这里对对象的操作,实际上是对代理类的操作
//SaveChanges真实的保存到数据库;SaveChanges会返回一个影响行数
int sum = My.SaveChanges();
if (sum > 0)
Response.Write("<script>alert('添加成功')</script>");
sele();//重新绑定Repeater控件
}
创建WebForm2.aspx展示文章页面↓
<style type="text/css">
*{
text-decoration:none;
color:black;
}
.auto-style2 {
width: 617px;
margin: 0px auto;
}
.auto-style3 {
width: 616px;
}
td {
width:610px;
height:80px;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
}
.bw{
margin-left:60%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style2">
类型:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:HyperLink ID="HyperLink2" CssClass="bw" runat="server" NavigateUrl="~/WebForm1.aspx">写博文</asp:HyperLink>
<hr />
<table class="auto-style3" style="table-layout:fixed;">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<th class="auto-style1">
<h2 style="text-align: left;"><%# Eval("tide")%></h2>
</th>
</tr>
<tr>
<th class="auto-style1" style="text-align: left;">
<%# Eval("auathor")%>
<samp><%# Eval("time")%></samp>
</th>
</tr>
<tr>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/WebForm3.aspx?id="+Eval("id")%>'><%# Eval("content")%></asp:HyperLink>
<hr />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
public partial class WebForm2 : System.Web.UI.Page
{
//创建一个EF数据上下文对象
MyBKEntities1 My = new MyBKEntities1();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定Repeater的数据源方法
sele();
//绑定DropDownList的数据源
this.DropDownList1.DataSource = My.Catelog.ToList();
this.DropDownList1.DataValueField = "Id";
this.DropDownList1.DataTextField = "Name";
this.DropDownList1.DataBind();
}
}
void sele()
{
//使用linq查询数据上下文Article表数据
var art = from article in My.Article
join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
select new
{
id = article.Id,
tide = article.tide,
auathor = article.Auathor,
content = article.Content,
time = article.time,
};//挑选出自己要的数据字段,在用匿名对象存放
// 将查询出的数据绑定到Repeater1控件中
this.Repeater1.DataSource = art.ToList();
this.Repeater1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//获取DropDownList1下标
int id = this.DropDownList1.SelectedIndex+1;
if (id == 1)
{
sele();
}
else
{
//使用linq查询数据上下文Article表数据
var art = from article in My.Article
join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
where catelog.Id == id
select new
{
id = article.Id,
tide = article.tide,
auathor = article.Auathor,
content = article.Content,
time = article.time,
};//挑选出自己要的数据字段,在用匿名对象存放
// 将查询出的数据绑定到Repeater1控件中
this.Repeater1.DataSource = art.ToList();
this.Repeater1.DataBind();
}
}
}
创建WebForm展示文章详情页面↓
<style type="text/css">
* {
/*HyperLink标签去下划线*/
text-decoration: none;
color: black;
}
.box {
width: 1000px;
margin: 0px auto;
}
.auto-style1 {
width: 728px;
}
.auto-style2 {
width: 797px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="box">
<table class="auto-style2">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<th class="auto-style1">
<h2 style="text-align: left;"><%# Eval("tide")%></h2>
</th>
</tr>
<tr>
<th class="auto-style1" style="text-align: left;">
<%# Eval("auathor")%>
<samp><%# Eval("time")%></samp>
</th>
</tr>
<tr>
<td>
<%# Eval("content")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
详情代码如下:
public partial class WebForm3 : System.Web.UI.Page
{
//创建一个EF数据上下文对象
MyBKEntities1 My = new MyBKEntities1();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = int.Parse(Request.QueryString["id"]);
//使用linq查询数据上下文Article表数据
var art = from article in My.Article
join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
where article.Id == id
select new
{
id = article.Id,
tide = article.tide,
auathor = article.Auathor,
content = article.Content,
time = article.time,
};//挑选出自己要的数据字段,在用匿名对象存放
// 将查询出的数据绑定到Repeater1控件中
this.Repeater1.DataSource = art.ToList();
this.Repeater1.DataBind();
}
}
}