📄 managenews.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace hotel.manage
{
/// <summary>
/// managenews 的摘要说明。
/// </summary>
public class managenews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected hotel.manage.DataSet5 dataSet51;
void bindData()
{
sqlDataAdapter1.Fill(dataSet51);
DataGrid1.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
bindData();
}
}
private void bind()
{
SqlConnection con =DB.createCon();
con.Open ();
SqlDataAdapter sda=new SqlDataAdapter ();
sda.SelectCommand =new SqlCommand ("select * from news",con);
DataSet ds=new DataSet ();
sda.Fill (ds,"news");
this.DataGrid1.DataKeyField="newsid";
this.DataGrid1.DataSource =ds.Tables ["news"];
this.DataGrid1.DataBind() ;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet51 = new hotel.manage.DataSet5();
((System.ComponentModel.ISupportInitialize)(this.dataSet51)).BeginInit();
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"TWJ-9490D5784F6\";packet size=4096;user id=sa;data source=\".\";pers" +
"ist security info=True;initial catalog=twj;password=sa";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "news", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("newsid", "newsid"),
new System.Data.Common.DataColumnMapping("title", "title"),
new System.Data.Common.DataColumnMapping("content", "content"),
new System.Data.Common.DataColumnMapping("newtime", "newtime"),
new System.Data.Common.DataColumnMapping("source", "source")})});
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO news(newsid, title, content, newtime, source) VALUES (@newsid, @title" +
", @content, @newtime, @source); SELECT newsid, title, content, newtime, source F" +
"ROM news";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@newsid", System.Data.SqlDbType.Int, 4, "newsid"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@title", System.Data.SqlDbType.VarChar, 100, "title"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@content", System.Data.SqlDbType.VarChar, 2147483647, "content"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@newtime", System.Data.SqlDbType.VarChar, 50, "newtime"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@source", System.Data.SqlDbType.VarChar, 100, "source"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT newsid, title, content, newtime, source FROM news";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// dataSet51
//
this.dataSet51.DataSetName = "DataSet5";
this.dataSet51.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet51)).EndInit();
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.bindData();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
((LinkButton)(e.Item.Cells[5].Controls[0])).Attributes.Add("onclick","return confirm('确认要删除吗?');");
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('确认要修改吗?');");
}
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int newsid=Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
SqlConnection con=DB.createCon();
SqlCommand cmd=new SqlCommand("delete from news where newsid='"+newsid+"'",con);
con.Open();
cmd.ExecuteNonQuery();
this.bindData();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
bindData();
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
Response.Redirect("addnews.aspx");
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
bindData();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con =DB.createCon();
con.Open ();
int newsid= Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
//非只读绑定列,处于编辑状态
string title = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;
string content = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string newtime = ((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string asource = ((TextBox)(e.Item.Cells[4].Controls[0])).Text;
// this.Response.Write(readersex);
string strSql = "update news set title = '"+title+"',content='"+content+"',newtime='"+newtime+"',source='"+asource+"'where newsid =" +newsid ;
// string strSql = "update 读者信息表 set readername = '" + readername + "',readsex='"+readersex+"',readerdep='"+readerdep+"',readertype='"+readertype+"'where readerid =" + readerid;
SqlCommand cmd=new SqlCommand (strSql,con);
try
{
cmd.ExecuteNonQuery();
this.Response.Write(" <script language ='javascript'> alert ('更新成功!')</script>");
this.DataGrid1.EditItemIndex=-1; //退出行的编辑模式
}
catch
{
this.Response.Write(" <script language ='javascript'> alert ('更新失败!')</script>");
}
this.DataGrid1.EditItemIndex=-1;
bindData();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -