//前台
<head runat="server">
<title></title>
<script type="text/javascript">
function showinfo() {
if (confirm('真的要删除吗') == false)
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="NewsTitle" HeaderText="标题" />
<asp:BoundField DataField="NewsContent" HeaderText="内容" />
<asp:BoundField DataField="RealName" HeaderText="创建者" />
<asp:BoundField DataField="CreateTime" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"
HeaderText="创建时间" />
<asp:BoundField DataField="ClassName" HeaderText="类别" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandArgument='<%#Eval("Id") %>' οnclick="btnEdit_Click">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandArgument='<%#Eval("Id") %>'
runat="server" οnclick="btnDelete_Click" OnClientClick="return showinfo();">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="btnFirst" runat="server" οnclick="btnFirst_Click">第一页 </asp:LinkButton>
<asp:LinkButton ID="btnPre" runat="server" οnclick="btnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" οnclick="btnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" οnclick="btnLast_Click">最后一页</asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>
//后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
int pagesize = 20;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack==false)
{
ViewState["pageindex"] = 1;
GetCount();
LoadData();
}
}
private void GetCount()
{
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
//cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId";
cmd.CommandText = " SELECT COUNT(*) FROM T_News1";
int totalcount=Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Dispose();
if (totalcount % pagesize == 0)
{
ViewState["pagelastindex"] = totalcount / pagesize;
}
else
{
ViewState["pagelastindex"] = totalcount / pagesize + 1;
}
}
private void LoadData()
{
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
//cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId";
cmd.CommandText = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY T1.Id)AS rownumber, T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId)A WHERE A.rownumber>(@pageindex-1)*@pagesize AND A.rownumber<=@pageindex*@pagesize";
cmd.Parameters.AddWithValue("@pageindex",ViewState["pageindex"]);
cmd.Parameters.AddWithValue("@pagesize", pagesize);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
//将查询出来的数据绑定到GridView
//原来需要手动拼接字符串的工作由GridView代劳了
this.GridView1.DataSource = dt;
//DataBind负责拼接字符串
this.GridView1.DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
protected void btnPre_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex > 1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex<Convert.ToInt32(ViewState["pagelastindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["pagelastindex"];
LoadData();
}
protected void btnEdit_Click(object sender, EventArgs e)
{
LinkButton btnEdit = sender as LinkButton;
Response.Redirect("WebEdit2.aspx?id=" + btnEdit.CommandArgument);
}
protected void btnDelete_Click(object sender, EventArgs e)
{
LinkButton btnDelete = sender as LinkButton;
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "DELETE FROM T_News1 WHERE Id=@id";
cmd.Parameters.AddWithValue("@id",btnDelete.CommandArgument);
if (cmd.ExecuteNonQuery() > 0)
{
LoadData();
}
cmd.Dispose();
conn.Dispose();
}
}
}
///
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebEdit2.aspx.cs" Inherits="WebApplication1.WebEdit2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
编号
</td>
<td>
<asp:TextBox ID="txtId" runat="server" Enabled="False" ReadOnly="True"
Width="254px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
标题
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
内容
</td>
<td>
<asp:TextBox ID="txtContent" runat="server" Height="223px" TextMode="MultiLine"
Width="259px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
类别
</td>
<td>
<asp:DropDownList ID="ddlClassName" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
用户
</td>
<td>
<asp:DropDownList ID="ddlUser" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnUpdate" runat="server" Text="保存" οnclick="btnUpdate_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebEdit2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"];
GetNews1(id);
}
}
//绑定用户
private void LoadUser()
{
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "SELECT * FROM T_User";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
this.ddlUser.DataSource = dt;
this.ddlUser.DataTextField = "RealName";
this.ddlUser.DataValueField = "UserId";
this.ddlUser.DataBind();
}
//绑定类别
private void LoadClass()
{
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "SELECT * FROM T_NewsClass";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
this.ddlClassName.DataSource = dt;
this.ddlClassName.DataTextField = "ClassName";
this.ddlClassName.DataValueField = "ClassId";
this.ddlClassName.DataBind();
}
private void GetNews1(string id)
{
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "SELECT * FROM T_News1 WHERE Id=@id";
cmd.Parameters.AddWithValue("@id",id);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
txtId.Text = dt.Rows[0]["Id"].ToString();
txtTitle.Text = dt.Rows[0]["NewsTitle"].ToString();
txtContent.Text = dt.Rows[0]["NewsContent"].ToString();
//绑定默认用户
LoadUser();
string userid= dt.Rows[0]["NewsCreator"].ToString();
foreach (ListItem item in this.ddlUser.Items)
{
if (item.Value == userid)
{
item.Selected = true;
}
}
//绑定默认类别
LoadClass();
string classid = dt.Rows[0]["ClassId"].ToString();
foreach (ListItem item in this.ddlClassName.Items)
{
if (item.Value == classid)
{
item.Selected = true;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
#region 获取用户输入和选择的值
string title = txtTitle.Text;
string content = txtContent.Text;
string userid = this.ddlUser.SelectedItem.Value;
string classid = this.ddlClassName.SelectedItem.Value;
#endregion
#region 更改数据
string strcon = @"Data Source=PC-YZC\SQLEXPRESS;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=1992051";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "UPDATE T_News1 SET NewsTitle=@newstitle,NewsContent=@newscontent,NewsCreator=@newscreator,ClassId=@classid WHERE Id=@id";
cmd.Parameters.AddWithValue("@newstitle",title);
cmd.Parameters.AddWithValue("@newscontent", content);
cmd.Parameters.AddWithValue("@newscreator", userid);
cmd.Parameters.AddWithValue("@classid", classid);
cmd.Parameters.AddWithValue("@id", txtId.Text);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Redirect("WebForm1.aspx");
}
#endregion
}
}
}