Repeater前台代码
<form id="form1" runat="server">
<div>
<%--添加功能--%>
<a href="Addinfo.aspx" class="btn btn-default">添加</a>
<table class="table table-bordered table-hover">
<tr class="info">
<td>编号</td>
<td>创建时间</td>
<td>昵称</td>
<td>简介</td>
<td>操作</td>
</tr>
<asp:Repeater runat="server" ID="rep" OnItemCommand="rep_ItemCommand">
<ItemTemplate>
<tr>
<td><%#Eval("id") %></td>
<td><%#Eval("createDate") %></td>
<td><%#Eval("nickname") %></td>
<td><%#Eval("introduce") %></td>
<td>
<asp:LinkButton runat="server" CssClass="btn btn-danger" CommandName="Updates" Text="修改" CommandArgument='<%#Eval("id") %>'></asp:LinkButton>
<asp:LinkButton runat="server" CssClass="btn btn-danger" OnClientClick="return confirm('是否删除?')" CommandName="Dels" Text="删除" CommandArgument='<%#Eval("id") %>'></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button runat ="server" OnClick="Unnamed_Click" Text="上一页" CssClass="btn btn-danger" />
<asp:Button runat ="server" OnClick="Unnamed_Click1" Text="下一页" CssClass="btn btn-danger" />
</div>
</form>
Repeater后台代码
string name = e.CommandName.ToString();
string id = e.CommandArgument.ToString();
if (name.Equals("Dels"))
{
bool isf = rj190xBLL.DeleteById(id);
if (isf)
{
Response.Write("<script>alert('删除成功');window.location.herf='index.aspx';</script>");
}
else
{
Response.Write("<script>alert('删除失败')</script>");
}
}
else
{
//重定向穿id
Response.Redirect("UpdateInfo.aspx?id="+id);
}
Repeater翻页功能实现
<asp:Button runat ="server" OnClick="Unnamed_Click" Text="上一页" CssClass="btn btn-danger" />
<asp:Button runat ="server" OnClick="Unnamed_Click1" Text="下一页" CssClass="btn btn-danger" />
后台实现
//定义全局翻页变量
static int pagenow = 1;
static int pagesize = 4;
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Unnamed_Click(object sender, EventArgs e)
{
if (pagenow<=1)
{
return;
}
pagenow--;
updatePage();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Unnamed_Click1(object sender, EventArgs e)
{
pagenow++;
updatePage();
}
public void updatePage()
{
rep.DataSource = rj190xBLL.SelectAll("", pagenow, pagesize);
rep.DataBind();
}
EF新增
前台
<form id="form1" runat="server">
<div>
<p>
昵称:<asp:TextBox runat ="server" ID="nickname" placeholder="请输入昵称" CssClass="form-control"></asp:TextBox>
</p>
<p>
简介:<asp:TextBox runat ="server" ID="introudice" placeholder="请输入简介" CssClass="form-control"></asp:TextBox>
</p>
<p>
<asp:button runat="server" OnClick="Unnamed_Click" CssClass="btn btn-group" Text="添加" />
</p>
</div>
</form>
后台
/// <summary>
/// 插入
/// </summary>
/// <param name="name">姓名</param>
/// <param name="introduct">格言</param>
/// <returns></returns>
public static bool InsertInfo(string name, string introduct)
{
using (rj190xEntities db = new rj190xEntities())
{
girl1907 g = new girl1907();
g.id = System.Guid.NewGuid().ToString("N");
g.createDate = DateTime.Now;
g.nickName = name;
g.introduce = introduct;
db.girl1907.Add(g);
int rows = db.SaveChanges();
return rows > 0;
}
}
EF修改
<form id="form1" runat="server">
<div>
修改
<p>
编号: <asp:label runat="server" ID="id"></asp:label>
</p>
<p>
创建时间: <asp:label runat="server" ID="DataTime"></asp:label>
</p>
<p>
昵称: <asp:TextBox runat="server" ID="nickname" placeholder="请输入昵称"></asp:TextBox>
</p>
<p>
简介: <asp:TextBox runat="server" ID="introduce" placeholder="请输入简介"></asp:TextBox>
</p>
<p>
<asp:Button runat="server" onclick="Unnamed_Click" Text="修改" CssClass="btn btn-default" />
</p>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
//重定向
if (!IsPostBack)
{
string id = Request.QueryString["id"];
if (string.IsNullOrEmpty(id))
{
Response.Redirect("index.aspx");
}
//绑定修改数据
girl1907 g = rj190xBLL.selectById(id);
this.id.Text = g.id;
this.DataTime.Text = g.createDate.ToString();
this.nickname.Text = g.nickName;
this.introduce.Text = g.introduce;
}
}
protected void Unnamed_Click(object sender, EventArgs e)
{
string id = this.id.Text;
string nickname = this.nickname.Text;
string introduce = this.introduce.Text;
//只要任何一个值为空 不操作
if (string.IsNullOrEmpty(id)| string.IsNullOrEmpty(nickname) | string.IsNullOrEmpty(introduce))
{
return;
}
bool isf = rj190xBLL.UpdateInfo(id,nickname,introduce);
if (isf)
{
Response.Write("<script>alert('修改成功!');window.location.href='index.aspx';</script>");
//Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('修改失败!');</script>");
}
}
DAL
/// <summary>
/// 修改
/// </summary>
/// <param name="id"></param>
/// <param name="nickname"></param>
/// <param name="introduct"></param>
/// <returns></returns>
public static bool UpdateInfo(string id, string nickName, string introduct)
{
using (rj190xEntities db = new rj190xEntities())
{
try
{
//如果查询的不止一个就会返回异常
girl1907 girl1907 = db.girl1907.Where(o => o.id == id).SingleOrDefault();
girl1907.nickName = nickName;
girl1907.introduce = introduct;
int rows = db.SaveChanges();
return rows > 0;
}
catch (Exception)
{
//捕捉异常解决id=空
return false;
}
}
}
EF删除
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool DeleteById(string id)
{
using (rj190xEntities db = new rj190xEntities())
{
try
{
//如果查询的不止一个就会返回异常
girl1907 girl1907 = db.girl1907.Where(o => o.id == id).SingleOrDefault();
db.girl1907.Remove(girl1907);
int rows = db.SaveChanges();
return rows > 0;
}
catch (Exception)
{
//捕捉异常解决id=空
return false;
}
}
}
EF查询
/// <summary>
/// 查找s
/// </summary>
/// <param name="nickName">模糊查询昵称</param>
/// <param name="pageNow">当前页</param>
/// <param name="pageSize">每页条数</param>
/// <returns></returns>
public static List<girl1907> SelectAll(string nickName, int pageNow, int pageSize)
{
using (rj190xEntities db = new rj190xEntities())
{
int rows = (pageNow - 1) * pageSize;
//查询所有 模糊查询
//List<girl1907> lists = db.girl1907.Where
// (o => o.nickName.Contains(nickName)).ToList();
//分页
List<girl1907> lists = db.girl1907.Where
(o => o.nickName.Contains(nickName)).OrderBy (o=>o.id) .Skip(rows).Take(pageSize).ToList();
return lists;
}
}