--这是查询新闻标题的前台页面
<div>
新闻标题:
<a href="WebAddaspx.aspx">添加</a>
<div id="divResult" runat="server"></div>
</div>
--这是查询新闻标题的后台代码
namespace 新闻管理系统
{
public partial class 用table显示信息 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["key"]))
{
TxtNewsTitle.Text = Request.QueryString["key"];
DataLoad();
}
if (Request.QueryString["ispostback"] == "1")
{
DataLoad();
}
}
}
private void DataLoad( )
{
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
#region 方法1
//using (SqlConnection con = new SqlConnection(Constr))
//{
// con.Open();
// string q = TxtNewsTitle.Text;
// string sql = string.Format("select * from T_News where NewsTitle like '%" + q + "%'", TxtNewsTitle.Text);
// using (SqlCommand cmd = new SqlCommand(sql, con))
// {
// using (SqlDataReader reader = cmd.ExecuteReader())
// {
// if (reader.HasRows)//hasreow属性,可以判断这次查询有没有查询结果集,有为true,否则false
// {
// while (reader.Read())
// {
// TableRow trRow = new TableRow();
// //获取第一条记录的每个列中的值
// //reader.GetValue(0).ToString();
// TableCell tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(0).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第2列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(1).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第3列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(2).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第4列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(3).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第5列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(4).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第6列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(5).ToString();
// trRow.Cells.Add(tcCell);
// Table1.Rows.Add(trRow);
// }
// }
// }
// }
//}
#endregion
#region 方法2
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,T1.NewsContent,T2.ClassName,T3.RealName,T4.Department_Name FROM T_News T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId INNER JOIN Department T4 ON T3.Department=T4.Department_Id WHERE T1.NewsTitle LIKE @newstitle Order by T1.Id DESC";
cmd.Parameters.AddWithValue("@newstitle", "%" + TxtNewsTitle.Text + "%");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
//将数据拼接成字符串发送到前台
StringBuilder sb1 = new StringBuilder();
string newstitle = string.Empty;
string newscontent = string.Empty;
string newsclass = string.Empty;
string creator = string.Empty;
string department = string.Empty;
sb1.Append("<table border="1">");
foreach (DataRow row in dt.Rows)
{
sb1.Append("<tr>");
newstitle = row["NewsTitle"].ToString();
newscontent = row["NewsContent"].ToString();
newsclass = row["ClassName"].ToString();
creator = row["RealName"].ToString();
department = row["Department_Name"].ToString();
sb1.Append("<td>" + newstitle + "</td>");
sb1.Append("<td>" + newscontent + "</td>");
sb1.Append("<td>" + newsclass + "</td>");
sb1.Append("<td>" + creator + "</td>");
sb1.Append("<td>" + department + "</td>");
sb1.Append("<td><a href="NewsAdd.aspx?id=" newstitle="">编辑</a></td>");
sb1.Append("<td><a href="NewsUpdate.aspx?id="> 更改</a></td>");
sb1.Append("<td><a href="Add.aspx?id=" newstitle="">添加</a></td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
divResult.InnerHtml = sb1.ToString();
}
}
#endregion
}
protected void Button1_Click(object sender, EventArgs e)
{
#region 验证输入是否为空
if (string.IsNullOrEmpty(TxtNewsTitle.Text))
{
TxtNewsTitle.Text = "请输入您要查询的数据";
return;
}
#endregion
DataLoad();
}
}
}
---这是新闻信息更改的前台页面
<div>
<table style="width: 100%;">
<tr>
<td class="style1">
新闻标题:
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
新闻类别:
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
发表人:
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style1">
新闻内容:
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style3">
</td>
</tr>
</table>
</div>
---这是新闻信息更改的后台代码
namespace 新闻管理系统
{
public partial class NewsUpdate : System.Web.UI.Page
{
string newsid = string.Empty;
string key = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{ newsid = Request.QueryString["id"];
key = Request.QueryString["key"];
if (!IsPostBack)
{
DataLoad(newsid);
}
}
private void DataLoad(string newsid)
{
//根据传递过来的新闻ID获取数据
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,T1.NewsContent,T1.ClassId,T2.ClassName,T3.RealName,T3.UserId,T4.Department_Name FROM T_News T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId INNER JOIN Department T4 ON T3.Department=T4.Department_Id WHERE T1.Id=@id";
cmd.Parameters.AddWithValue("@id",newsid);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
/*将查询出来的数据绑定到界面控件上*/
TxtNewsTitle.Text = dt.Rows[0]["NewsTitle"].ToString();
TxtContent.Text = dt.Rows[0]["NewsContent"].ToString();
/*将新闻类别绑定到新闻类别下拉列表中*/
cmd.Parameters.Clear();
cmd.CommandText = "select UserId,RealName from T_User";
DataTable dtName = new DataTable();
adapter.Fill(dtName);
#region 方法1: /*将创建者绑定到下拉*/
//this.DropDownList1.DataSource = dtName;
//this.DropDownList1.DataTextField = "RealName";
//this.DropDownList1.DataValueField = "UserId";
//this.DropDownList1.DataBind();
//foreach (ListItem item in DropDownList1.Items)
//{
// if (item.Text == dt.Rows[0]["RealName"].ToString())
// {
// item.Selected = true;
// }
//}
#endregion
//方法2
this.DropDownList1.DataSource = dtName;
this.DropDownList1.DataTextField = "RealName";
this.DropDownList1.DataValueField = "UserId";
this.DropDownList1.DataBind();
DropDownList1.Items.FindByValue(dt.Rows[0]["UserId"].ToString()).Selected = true;
/*将新闻类别绑定新闻类别的下拉列表中D*/
cmd.Parameters.Clear();
cmd.CommandText = "SELECT ClassId,ClassName FROM T_NewsClass";
DataTable dtNewsClass = new DataTable();
adapter.Fill(dtNewsClass);
#region 方法1 /*将新闻类别绑定到下拉列表*/
//this.DropDownList2.DataSource = dtNewsClass;
//this.DropDownList2.DataTextField = "ClassName";
//this.DropDownList2.DataValueField = "ClassId";
//this.DropDownList2.DataBind();
//foreach (ListItem item in DropDownList2.Items)
//{
// if (item.Text == dt.Rows[0]["ClassName"].ToString())
// {
// item.Selected = true;
// }
//}
#endregion
#region 方法2
this.DropDownList2.DataSource = dtNewsClass;
this.DropDownList2.DataTextField = "ClassName";
this.DropDownList2.DataValueField = "ClassId";
this.DropDownList2.DataBind();
DropDownList2.Items.FindByValue(dt.Rows[0]["ClassId"].ToString()).Selected = true;
#endregion
cmd.Dispose();
conn.Dispose();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string newstitle = TxtNewsTitle.Text;
string newscontent = TxtContent.Text;
string classid = DropDownList2.SelectedItem.Value;
string userid = DropDownList1.SelectedItem.Value;
//建立数据连接,更改数据
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "UPDATE T_News SET NewsTitle=@newstitle,NewsContent=@newscontent,ClassId=@classid,NewsCreator=@newscreator WHERE Id=@id";
cmd.Parameters.AddWithValue("@newstitle", newstitle);
cmd.Parameters.AddWithValue("@newscontent", newscontent);
cmd.Parameters.AddWithValue("@classid", classid);
cmd.Parameters.AddWithValue("@newscreator", userid);
cmd.Parameters.AddWithValue("@id", newsid);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Redirect("用table显示信息.aspx?key=" + key);
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
--这是新闻信息的添加页面
<div>
<table>
<tr>
<td>
新闻标题:
</td>
<td>
</td>
</tr>
<tr>
<td>
新闻类别:
</td>
<td>
</td>
</tr>
<tr>
<td>
创建者:
</td>
<td>
</td>
</tr>
<tr>
<td>
新闻内容:
</td>
<td>
</td>
</tr>
<tr><td>
</td><td></td></tr>
</table>
</div>
--这是新闻信息的添加页面后台代码
namespace 新闻管理系统
{
public partial class WebAddaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadClassName();
LoadUsers();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
#region 获取界面上控件的值
string newstitle = txtNewsTitle.Text;
string newscontent = txtNewsContent.Text;
string classid = ddlClassName.SelectedItem.Value;
string userid = ddlCreator.SelectedItem.Value;
#endregion
#region 建立数据库连接,插入数据
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO T_News(NewsTitle,NewsContent,NewsCreator,CreateTime,ClassId) VALUES(@newstitle,@newscontent,@newscreator,GETDATE(),@calssid)";
cmd.Parameters.AddWithValue("@newstitle", newstitle);
cmd.Parameters.AddWithValue("@newscontent", newscontent);
cmd.Parameters.AddWithValue("@newscreator", userid);
cmd.Parameters.AddWithValue("@calssid", classid);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Redirect("用table显示信息.aspx?ispostback=1");
}
cmd.Dispose();
conn.Close();
conn.Dispose();
#endregion
}
private void LoadClassName()
{
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT ClassId,ClassName From T_NewsClass";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Close();
conn.Dispose();
this.ddlClassName.DataSource = dt;
this.ddlClassName.DataTextField = "ClassName";
this.ddlClassName.DataValueField = "ClassId";
this.ddlClassName.DataBind();
}
private void LoadUsers()
{
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT UserId,RealName From T_User";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.ddlCreator.DataSource = dt;
this.ddlCreator.DataTextField = "RealName";
this.ddlCreator.DataValueField = "UserId";
this.ddlCreator.DataBind();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
<div>
新闻标题:
<a href="WebAddaspx.aspx">添加</a>
<div id="divResult" runat="server"></div>
</div>
--这是查询新闻标题的后台代码
namespace 新闻管理系统
{
public partial class 用table显示信息 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["key"]))
{
TxtNewsTitle.Text = Request.QueryString["key"];
DataLoad();
}
if (Request.QueryString["ispostback"] == "1")
{
DataLoad();
}
}
}
private void DataLoad( )
{
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
#region 方法1
//using (SqlConnection con = new SqlConnection(Constr))
//{
// con.Open();
// string q = TxtNewsTitle.Text;
// string sql = string.Format("select * from T_News where NewsTitle like '%" + q + "%'", TxtNewsTitle.Text);
// using (SqlCommand cmd = new SqlCommand(sql, con))
// {
// using (SqlDataReader reader = cmd.ExecuteReader())
// {
// if (reader.HasRows)//hasreow属性,可以判断这次查询有没有查询结果集,有为true,否则false
// {
// while (reader.Read())
// {
// TableRow trRow = new TableRow();
// //获取第一条记录的每个列中的值
// //reader.GetValue(0).ToString();
// TableCell tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(0).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第2列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(1).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第3列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(2).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第4列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(3).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第5列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(4).ToString();
// trRow.Cells.Add(tcCell);
// //获取第一条记录的第6列中的值
// //reader.GetValue(0).ToString();
// tcCell = new TableCell();
// tcCell.BorderStyle = BorderStyle.Solid;
// tcCell.BorderWidth = 1;
// tcCell.Text = reader.GetValue(5).ToString();
// trRow.Cells.Add(tcCell);
// Table1.Rows.Add(trRow);
// }
// }
// }
// }
//}
#endregion
#region 方法2
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,T1.NewsContent,T2.ClassName,T3.RealName,T4.Department_Name FROM T_News T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId INNER JOIN Department T4 ON T3.Department=T4.Department_Id WHERE T1.NewsTitle LIKE @newstitle Order by T1.Id DESC";
cmd.Parameters.AddWithValue("@newstitle", "%" + TxtNewsTitle.Text + "%");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
//将数据拼接成字符串发送到前台
StringBuilder sb1 = new StringBuilder();
string newstitle = string.Empty;
string newscontent = string.Empty;
string newsclass = string.Empty;
string creator = string.Empty;
string department = string.Empty;
sb1.Append("<table border="1">");
foreach (DataRow row in dt.Rows)
{
sb1.Append("<tr>");
newstitle = row["NewsTitle"].ToString();
newscontent = row["NewsContent"].ToString();
newsclass = row["ClassName"].ToString();
creator = row["RealName"].ToString();
department = row["Department_Name"].ToString();
sb1.Append("<td>" + newstitle + "</td>");
sb1.Append("<td>" + newscontent + "</td>");
sb1.Append("<td>" + newsclass + "</td>");
sb1.Append("<td>" + creator + "</td>");
sb1.Append("<td>" + department + "</td>");
sb1.Append("<td><a href="NewsAdd.aspx?id=" newstitle="">编辑</a></td>");
sb1.Append("<td><a href="NewsUpdate.aspx?id="> 更改</a></td>");
sb1.Append("<td><a href="Add.aspx?id=" newstitle="">添加</a></td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
divResult.InnerHtml = sb1.ToString();
}
}
#endregion
}
protected void Button1_Click(object sender, EventArgs e)
{
#region 验证输入是否为空
if (string.IsNullOrEmpty(TxtNewsTitle.Text))
{
TxtNewsTitle.Text = "请输入您要查询的数据";
return;
}
#endregion
DataLoad();
}
}
}
---这是新闻信息更改的前台页面
<div>
<table style="width: 100%;">
<tr>
<td class="style1">
新闻标题:
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
新闻类别:
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
发表人:
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style1">
新闻内容:
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style3">
</td>
</tr>
</table>
</div>
---这是新闻信息更改的后台代码
namespace 新闻管理系统
{
public partial class NewsUpdate : System.Web.UI.Page
{
string newsid = string.Empty;
string key = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{ newsid = Request.QueryString["id"];
key = Request.QueryString["key"];
if (!IsPostBack)
{
DataLoad(newsid);
}
}
private void DataLoad(string newsid)
{
//根据传递过来的新闻ID获取数据
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,T1.NewsContent,T1.ClassId,T2.ClassName,T3.RealName,T3.UserId,T4.Department_Name FROM T_News T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId INNER JOIN Department T4 ON T3.Department=T4.Department_Id WHERE T1.Id=@id";
cmd.Parameters.AddWithValue("@id",newsid);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
/*将查询出来的数据绑定到界面控件上*/
TxtNewsTitle.Text = dt.Rows[0]["NewsTitle"].ToString();
TxtContent.Text = dt.Rows[0]["NewsContent"].ToString();
/*将新闻类别绑定到新闻类别下拉列表中*/
cmd.Parameters.Clear();
cmd.CommandText = "select UserId,RealName from T_User";
DataTable dtName = new DataTable();
adapter.Fill(dtName);
#region 方法1: /*将创建者绑定到下拉*/
//this.DropDownList1.DataSource = dtName;
//this.DropDownList1.DataTextField = "RealName";
//this.DropDownList1.DataValueField = "UserId";
//this.DropDownList1.DataBind();
//foreach (ListItem item in DropDownList1.Items)
//{
// if (item.Text == dt.Rows[0]["RealName"].ToString())
// {
// item.Selected = true;
// }
//}
#endregion
//方法2
this.DropDownList1.DataSource = dtName;
this.DropDownList1.DataTextField = "RealName";
this.DropDownList1.DataValueField = "UserId";
this.DropDownList1.DataBind();
DropDownList1.Items.FindByValue(dt.Rows[0]["UserId"].ToString()).Selected = true;
/*将新闻类别绑定新闻类别的下拉列表中D*/
cmd.Parameters.Clear();
cmd.CommandText = "SELECT ClassId,ClassName FROM T_NewsClass";
DataTable dtNewsClass = new DataTable();
adapter.Fill(dtNewsClass);
#region 方法1 /*将新闻类别绑定到下拉列表*/
//this.DropDownList2.DataSource = dtNewsClass;
//this.DropDownList2.DataTextField = "ClassName";
//this.DropDownList2.DataValueField = "ClassId";
//this.DropDownList2.DataBind();
//foreach (ListItem item in DropDownList2.Items)
//{
// if (item.Text == dt.Rows[0]["ClassName"].ToString())
// {
// item.Selected = true;
// }
//}
#endregion
#region 方法2
this.DropDownList2.DataSource = dtNewsClass;
this.DropDownList2.DataTextField = "ClassName";
this.DropDownList2.DataValueField = "ClassId";
this.DropDownList2.DataBind();
DropDownList2.Items.FindByValue(dt.Rows[0]["ClassId"].ToString()).Selected = true;
#endregion
cmd.Dispose();
conn.Dispose();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string newstitle = TxtNewsTitle.Text;
string newscontent = TxtContent.Text;
string classid = DropDownList2.SelectedItem.Value;
string userid = DropDownList1.SelectedItem.Value;
//建立数据连接,更改数据
string Constr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
using (SqlConnection conn = new SqlConnection(Constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "UPDATE T_News SET NewsTitle=@newstitle,NewsContent=@newscontent,ClassId=@classid,NewsCreator=@newscreator WHERE Id=@id";
cmd.Parameters.AddWithValue("@newstitle", newstitle);
cmd.Parameters.AddWithValue("@newscontent", newscontent);
cmd.Parameters.AddWithValue("@classid", classid);
cmd.Parameters.AddWithValue("@newscreator", userid);
cmd.Parameters.AddWithValue("@id", newsid);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Redirect("用table显示信息.aspx?key=" + key);
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
--这是新闻信息的添加页面
<div>
<table>
<tr>
<td>
新闻标题:
</td>
<td>
</td>
</tr>
<tr>
<td>
新闻类别:
</td>
<td>
</td>
</tr>
<tr>
<td>
创建者:
</td>
<td>
</td>
</tr>
<tr>
<td>
新闻内容:
</td>
<td>
</td>
</tr>
<tr><td>
</td><td></td></tr>
</table>
</div>
--这是新闻信息的添加页面后台代码
namespace 新闻管理系统
{
public partial class WebAddaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadClassName();
LoadUsers();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
#region 获取界面上控件的值
string newstitle = txtNewsTitle.Text;
string newscontent = txtNewsContent.Text;
string classid = ddlClassName.SelectedItem.Value;
string userid = ddlCreator.SelectedItem.Value;
#endregion
#region 建立数据库连接,插入数据
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO T_News(NewsTitle,NewsContent,NewsCreator,CreateTime,ClassId) VALUES(@newstitle,@newscontent,@newscreator,GETDATE(),@calssid)";
cmd.Parameters.AddWithValue("@newstitle", newstitle);
cmd.Parameters.AddWithValue("@newscontent", newscontent);
cmd.Parameters.AddWithValue("@newscreator", userid);
cmd.Parameters.AddWithValue("@calssid", classid);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Redirect("用table显示信息.aspx?ispostback=1");
}
cmd.Dispose();
conn.Close();
conn.Dispose();
#endregion
}
private void LoadClassName()
{
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT ClassId,ClassName From T_NewsClass";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Close();
conn.Dispose();
this.ddlClassName.DataSource = dt;
this.ddlClassName.DataTextField = "ClassName";
this.ddlClassName.DataValueField = "ClassId";
this.ddlClassName.DataBind();
}
private void LoadUsers()
{
string strcon = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT UserId,RealName From T_User";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.ddlCreator.DataSource = dt;
this.ddlCreator.DataTextField = "RealName";
this.ddlCreator.DataValueField = "UserId";
this.ddlCreator.DataBind();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}