sss
详情表:
类别表:
视图层:
<div>
<h3 style="text-align:center">添加商品</h3>
标题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></br></br>
发布人:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></br></br>
内容:<asp:TextBox ID="TextBox3" runat="server" Height="110px" style="margin-bottom: 0px" Width="197px" TextMode="MultiLine"></asp:TextBox></br></br>
类型:<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">情感</asp:ListItem>
<asp:ListItem Value="2">小说</asp:ListItem>
<asp:ListItem Value="3">技术</asp:ListItem>
<asp:ListItem Value="4">杂谈</asp:ListItem>
<asp:ListItem Value="5">扯淡</asp:ListItem>
<asp:ListItem Value="6">夜话</asp:ListItem>
<asp:ListItem Value="7">C#</asp:ListItem>
</asp:DropDownList></br></br>
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" style="height: 21px" />
<table border="1">
<tr>
<th>标题</th>
<th>发布人</th>
<th>内容</th>
<th>类型</th>
<th>详情</th>
<th>操作</th>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td><%# Eval("Title")%></td>
<td><%# Eval("Author")%></td>
<td><%# Eval("Content")%></td>
<td><%# Eval("CatelogName")%></td>
<td><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm2.aspx">详情</asp:HyperLink></td>
<td><asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("id")%>' CommandName="delete">删除</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
代码:
MyDBEntities db = new MyDBEntities();
//数据库链接
protected void Page_Load(object sender, EventArgs e)
{
var result = from article in db.Article
join Catelog in db.Catelog on article.Catelogid equals Catelog.id
select new
{
id = article.id,
Title = article.Title,
Content = article.Content,
Author = article.Author,
CatelogName = Catelog.Name
};
//连表获取数据集
Repeater1.DataSource = result.ToList();
Repeater1.DataBind();
//数据控件绑定
}
protected void Button1_Click(object sender, EventArgs e)
{
Article article = new Article();
article.Title = TextBox1.Text;
article.Author = TextBox2.Text;
article.Content = TextBox3.Text;
article.Catelogid = int.Parse(DropDownList1.SelectedValue);
//绑定控件传值
db.Article.Add(article);
int count = db.SaveChanges();
if (count >= 0)
{
Response.Write("<script>alert('添加成功')</script>");
Page_Load(sender, e);
//刷新界面
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
int id = int.Parse(e.CommandArgument.ToString());
int count = Del(id);
if (count > 0)
{
Response.Write("<script>alert('删除成功!')</script>");
MyDBEntities db = new MyDBEntities();
}
}
}
//方法
public int Del(int id)
{
var FUCK = (from u in db.Article
where u.id == id
select u).FirstOrDefault();
db.Article.Remove(FUCK);
return db.SaveChanges();
}