我的小知识

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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值