public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = “吴斌有限公司登录界面”;
}
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = "server=.;uid=sa;pwd=123456;database=NewsManagement";
SqlConnection cn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("select * from [Admin] where UserName='" + txtUserName.Text + "' and UserPassword='" + txtUserPassword.Text + "'", cn);
cn.Open();
if (cmd.ExecuteScalar() == null)
{
Response.Write("<script>alert('登录失败!,用户名或者密码错误!如果没有账号,请注册!');</script>");
}
else
{
Response.Write("<script>alert('登录成功!');</script>;");
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Session["user"] = sdr["uid"].ToString();
}
Response.Redirect("back_index.aspx");
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("ProductList.aspx");
}
public partial class News : System.Web.UI.Page
{
const string constr = “server=.;uid=sa;pwd=123456;database=NewsManagement”;
SqlConnection conn = new SqlConnection(constr);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session[“user”] == null)
{
Response.Write("");
}
else
{
GetDropLost();
string ID = Session["user"].ToString();
string strsql = "select * from Admin where uid='" + ID + "'";
SqlCommand cmd1 = new SqlCommand(strsql, conn); conn.Open();
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{
lblName.Text = dr["UserName"].ToString();
}
conn.Close();
if (!IsPostBack)
{
Dataload();
}
}
}
}
public void Dataload(string searchsql = "")
{
string sqlselect = "select * , NewsType from News,NewsType where News.Typeid=NewsType.Typeid order by News.Ntime desc";
if (searchsql.Trim() != "and")
{
sqlselect = "select * , NewsType from News,NewsType where News.Typeid=NewsType.Typeid " + searchsql + "order by News.Ntime desc";
}
//string sqlselect = "select * , NewsType from News,NewsType ";
conn.Open();
DataSet da = new DataSet();
SqlDataAdapter data = new SqlDataAdapter(sqlselect, conn);
data.Fill(da);
GridView1.DataSource = da;
GridView1.DataBind();
conn.Close();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
StringBuilder strsql = new StringBuilder();
if (!String.IsNullOrEmpty(txtTitle.Text))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntop like '%{0}%'", txtTitle.Text);
}
if (!String.IsNullOrEmpty(txtAuthor.Text))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Nname like '%{0}%'", txtAuthor.Text);
}
if (!String.IsNullOrEmpty(TimeStart.Value))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntime>='{0}'", TimeStart.Value);
}
if (!String.IsNullOrEmpty(TimeEnd.Value))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntime<='{0}'", TimeEnd.Value);
}
if (DropDownList1.Text != "--请选择--")
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Typeid like '{0}'", DropDownList1.Text);
}
Dataload("and " + strsql.ToString());
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
try
{
string strsql = "delete from News where [id]='" + id + "'";
SqlCommand cmd = new SqlCommand(strsql, conn);
conn.Open();
cmd.ExecuteReader();
conn.Close();
Response.Write("<script language=javascript>alert('删除成功!');</script>");
Dataload();
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('删除失败!发生如下错误:" + ex.Message + "');</script>");
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Dataload();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string id = GridView1.DataKeys[e.NewEditIndex]["id"].ToString();
Response.Redirect("NewsEdit.aspx?id=" + id);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].ToolTip = e.Row.Cells[1].Text;
if (e.Row.Cells[1].Text.Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 10) + "...";
}
}
}
protected void btnClose_Click(object sender, EventArgs e)
{
Session["user"] = null;
Response.Redirect("Login.aspx");
}
protected void btnjoin_Click(object sender, EventArgs e)
{
Response.Redirect("NewAdd.aspx");
}
protected void btnCaregory_Click(object sender, EventArgs e)
{
Response.Redirect("NewsTypeAdd.aspx");
}
void GetDropLost()
{
conn.Open();
string SQL = "select * from NewsType";
SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
System.Data.DataSet ds = new System.Data.DataSet();
sda.Fill(ds);
DropDownList1.DataValueField = "Typeid";
DropDownList1.DataTextField = "NewsType";
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem("--请选择--"));
conn.Close();
}
public partial class NewsList : System.Web.UI.Page
{
const string constr = “server=.;uid=sa;pwd=123456;database=NewsManagement”;
SqlConnection conn = new SqlConnection(constr);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session[“user”] == null)
{
Response.Write("");
}
else
{
GetDropLost();
string ID = Session["user"].ToString();
string strsql = "select * from Admin where uid='" + ID + "'";
SqlCommand cmd1 = new SqlCommand(strsql, conn); conn.Open();
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{
lblName.Text = dr["UserName"].ToString();
}
conn.Close();
if (!IsPostBack)
{
Dataload();
}
}
}
}
public void Dataload( string searchsql="")
{
string sqlselect = "select * , NewsType from News,NewsType where News.Typeid=NewsType.Typeid order by News.Ntime desc";
if (searchsql.Trim() != "and")
{
sqlselect = "select * , NewsType from News,NewsType where News.Typeid=NewsType.Typeid " + searchsql + "order by News.Ntime desc";
}
//string sqlselect = "select * , NewsType from News,NewsType ";
conn.Open();
DataSet da = new DataSet();
SqlDataAdapter data = new SqlDataAdapter(sqlselect, conn);
data.Fill(da);
GridView1.DataSource = da;
GridView1.DataBind();
conn.Close();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
StringBuilder strsql = new StringBuilder();
if (!String.IsNullOrEmpty(txtTitle.Text))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntop like '%{0}%'", txtTitle.Text);
}
if (!String.IsNullOrEmpty(txtAuthor.Text))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Nname like '%{0}%'", txtAuthor.Text);
}
if (!String.IsNullOrEmpty(TimeStart.Value))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntime>='{0}'", TimeStart.Value);
}
if (!String.IsNullOrEmpty(TimeEnd.Value))
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Ntime<='{0}'", TimeEnd.Value);
}
if (DropDownList1.Text != "--请选择--")
{
if (!string.IsNullOrEmpty(strsql.ToString()))
{
strsql.AppendFormat("and");
}
strsql.AppendFormat(" Typeid like '{0}'", DropDownList1.Text);
}
Dataload( "and "+ strsql.ToString());
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
try
{
string strsql = "delete from News where [id]='" + id + "'";
SqlCommand cmd = new SqlCommand(strsql, conn);
conn.Open();
cmd.ExecuteReader();
conn.Close();
Response.Write("<script language=javascript>alert('删除成功!');</script>");
Dataload();
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('删除失败!发生如下错误:" + ex.Message + "');</script>");
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Dataload();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string id = GridView1.DataKeys[e.NewEditIndex]["id"].ToString();
Response.Redirect("NewsEdit.aspx?id=" + id);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].ToolTip = e.Row.Cells[1].Text;
if (e.Row.Cells[1].Text.Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 10) + "...";
}
}
}
protected void btnClose_Click(object sender, EventArgs e)
{
Session["user"] = null;
Response.Redirect("Login.aspx");
}
protected void btnjoin_Click(object sender, EventArgs e)
{
Response.Redirect("NewAdd.aspx");
}
protected void btnCaregory_Click(object sender, EventArgs e)
{
Response.Redirect("NewsTypeAdd.aspx");
}
void GetDropLost()
{
conn.Open();
string SQL = "select * from NewsType";
SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
System.Data.DataSet ds = new System.Data.DataSet();
sda.Fill(ds);
DropDownList1.DataValueField = "Typeid";
DropDownList1.DataTextField = "NewsType";
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem("--请选择--"));
conn.Close();
}