newsManager.aspx.cs
上传用户:xy99169
上传日期:2022-08-03
资源大小:139k
文件大小:4k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
ASP/ASPX
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- using System.Web.Configuration;
- public partial class newsManager : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- bindNews();
- }
- }
- protected void bindNews()
- {
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "select * from news";
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- try
- {
- conn.Open();
- SqlDataReader sdr = cmd.ExecuteReader();
- listNews.DataSource = sdr;
- //listNews.DataTextField = "newsTitle";
- //listNews.DataValueField = "newsID";
- listNews.DataBind();
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "Insert news(newsTitle,newsContent) Values(@title,@content)";
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- cmd.Parameters.AddWithValue("@title", TextBox1.Text);
- cmd.Parameters.AddWithValue("@content", TextBox2.Text);
- try
- {
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- bindNews();
- TextBox1.Text = "";
- TextBox2.Text = "";
- }
- protected void listNews_SelectedIndexChanged(object sender, EventArgs e)
- {
- string id = listNews.SelectedValue;
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "select * from news where newsid="+id;
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- try
- {
- conn.Open();
- SqlDataReader sdr = cmd.ExecuteReader();
- if (sdr.Read())
- {
- Label1.Text = "新闻编号:" + sdr["newsID"].ToString() + "<br/>";
- Label1.Text += "新闻标题:" + sdr["newsTitle"].ToString() + "<br/>";
- Label1.Text += "新闻内容:" + sdr["newsContent"].ToString() + "<br/>";
- }
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string id = listNews.SelectedValue;
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "Delete news where newsid="+id;
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- try
- {
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- bindNews();
- Label1.Text = "";
- }
- }