C#语言,关于在EF框架中利用LINQ语法完成Web页面中的增加,删除,修改等操作
结合Repeater和Dropdownlist下拉框绑定数据展示完成功能
1.在Web项目中创建ADO.NET实体模型
2.选择服务器(.)默认 在选择输入数据库中选择你需要的数据库源
3.添加两张表新建关联 (因为两张表设置了外键)且需要注意的是两张表直接的关系,
是一对多,多对多,一对一关系的其中一种,假设需要在删除一张表数据的同时删除另外一张表的数据则需要在表设计的时候就添加级联关系
3.在源码中的代码如下 分别设置了添加button按钮 linkbutton2删除 和 linkbutton3修改
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" PostBackUrl="~/Add.aspx" runat="server">添加</asp:LinkButton>
<table border="1" style="border-style: none; text-align:center; height: 213px; width: 444px;">
<tr>
<td>标题</td>
<td>作者</td>
<td>内容</td>
<td>分类</td>
<td>操作</td>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td><%# Eval("title") %></td>
<td><%# Eval("author") %></td>
<td><%# Eval("context") %></td>
<td><%# Eval("catelogName") %></td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" OnClientClick="return window.confirm('确认删除吗?')" CommandArgument='<%# Eval("id") %>'>删除</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="update" CommandArgument='<%# Eval("id") %>'>修改</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
4.关于Repeater1的数据绑定(后台代码)DB1902Entities1 db = new DB1902Entities1();用于获取数据库的实体对象db
db.Article.FirstOrDefault(p => p.id == 1)为LINQ表达式用于获取到db数据库中Article表的指定数据(p类似于Article表)
再利用articleList集合装载Article对象赋予控件绑定数据,再RepeaterBind()事件中result利用了LINQ的两表联查语法同时获取
两张表的数据
DB1902Entities1 db = new DB1902Entities1();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["msg"] != null)
{
string msg = Request.QueryString["msg"].ToString();
Response.Write("<script>alert('" + msg + "')</script>");
}
DB1902Entities1 db = new DB1902Entities1();
//List<Article> articleList=new List<Article>();
//articleList.Add(db.Article.FirstOrDefault(p => p.id == 1));
//this.Repeater1.DataSource = articleList;
RepeaterBind();
}
}
public void RepeaterBind()
{var result = from a in db.Article
join c in db.Catelog on a.catebgid equals c.id
select new { id = a.id, title = a.title, author = a.author, context = a.context, catelogName = c.name };
this.Repeater1.DataSource = result.ToList();
this.Repeater1.DataBind();
}5.在Repeater1的Repeater1_ItemCommand事件中编写Response.Redirect跳转语句(后台代码)用于跳转到各页面中
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int id=int.Parse(e.CommandArgument.ToString());
if (e.CommandName=="delete")
{
var result = db.Article.Where(a => a.id == id).FirstOrDefault();
db.Article.Remove(result);
int count=db.SaveChanges();
if (count>0)
{
Response.Write("<script>alert('删除成功!')</script>");
RepeaterBind();
}
}
if (e.CommandName=="update")
{
Response.Redirect("Update.aspx?id="+id+"");
}
}
6.再分别新建添加页面和修改页面,添加页面如下(后台代码)
附上源码
//添加页面 后台代码
public partial class Add : System.Web.UI.Page
{
DB1902Entities1 db = new DB1902Entities1();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownListBind();
}
}
public void DropDownListBind()
{
DropDownList1.DataSource = db.Catelog.ToList();
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Article article = new Article();
article.title = TextBox1.Text;
article.context = TextBox2.Text;
article.author = TextBox3.Text;
article.catebgid =int.Parse(DropDownList1.SelectedValue);
db.Article.Add(article);
db.SaveChanges();
if (db.SaveChanges() >= 0)
{
Response.Redirect("WebForm1.aspx?msg=添加成功!");
}
}
//修改页面 后台代码(源码与新增页面类似)FirstOrDefault获取符合条件的第一条数据
通过下拉框的选中获取catebgId(外键)的值
DB1902Entities1 db = new DB1902Entities1();
Article result;
string cateName;
protected void Page_Load(object sender, EventArgs e)
{
int id = int.Parse(Request["id"].ToString());
if (!IsPostBack)
{
result = db.Article.FirstOrDefault(a => a.id == id);
cateName = db.Catelog.FirstOrDefault(c => c.id == result.catebgid).name;
TextBox1.Text = result.title;
TextBox2.Text = result.context;
TextBox3.Text = result.author;
DropDownListBind();
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
if (DropDownList1.Items[i].Text == cateName)
{
DropDownList1.SelectedIndex = i;
}
}
}
}