通用后台设计

通用后台设计(一)
通用后台设计(一)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AfterEndDemo.Admin
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlNewsListBinds();
                gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
            }
        }

        //绑定新闻类别
        protected void ddlNewsListBinds()
        {
            ddlNewstype.DataSource = DBHelper.GetDataTable("select * from NewsType");
            ddlNewstype.DataTextField = "Type";
            ddlNewstype.DataValueField = "ID";
            ddlNewstype.DataBind();
            //在下拉框的顶端添加一个选项
            ddlNewstype.Items.Insert(0, new ListItem("全部", ""));
        }

        //绑定新闻列表
        protected void gvNewsListBinds(String type, String title)
        {
            String sql = "select * from News where 1=1";

            if (!String.IsNullOrEmpty(type))
            {
                sql += " and NewsType=" + type;
            }

            if (!String.IsNullOrEmpty(title))
            {
                sql += " and Title like '%" + title + "%'";
            }

            gvNewsList.DataSource = DBHelper.GetDataTable(sql);
            gvNewsList.DataBind();
        }

        //搜索
        protected void imbSearch_Click(object sender, ImageClickEventArgs e)
        {
            gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
        }

        //分页
        protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNewsList.PageIndex = e.NewPageIndex;
            gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
        }

        //在绑定行的时候激发
        protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.background='#FCFDEE'");
                e.Row.Attributes.Add("onmouseout","this.style.background='#FFFFFF'");
            }
        }

        protected void lbnDelete_Click(object sender, EventArgs e)
        {
            //用于获取表单元素指定的name的Value
            //如果有多个name相同的表单元素,将以","分隔
            string id = Request.Form["cbID"];
            if (String.IsNullOrEmpty(id))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('未选择任何数据!');", true);
                return;
            }

            string sql = "delete From News Where id in (" + id + ")";
            string msg = "删除失败";
            if (DBHelper.ExecuteCommand(sql)>0)
            {
                msg = "删除成功!";
                //重绑定
                gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
            }

            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg2", "alert('" + msg + "');location.href='NewsList.aspx'", true);

        }
        //新增
        protected void lbnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNews.aspx");
        }
        //本地编辑
        protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvNewsList.EditIndex = e.NewEditIndex;
            gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
        }
        //更新
        protected void gvNewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           TextBox txtTitle=gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;

           int id=Convert.ToInt32(gvNewsList.DataKeys[e.RowIndex].Value);
           
        }
        //取消
        protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvNewsList.EditIndex = -1;
            gvNewsListBinds(ddlNewstype.SelectedValue, txtTitle.Text);
        }

        protected void gvNewsList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            
        }
    }
}
通用后台设计(二)
通用后台设计(二)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace AfterEndDemo.Admin
{
    public partial class AddNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["id"] = Request.QueryString["id"];
                ddlNewsTypeBinds();
                NewsBind();
            }
        }
        //修改绑定值
        protected void NewsBind() 
        {
            if (ViewState["id"]!=null)
            {
                DataTable dt = DBHelper.GetDataTable("select * from News where id=" + Convert.ToInt32(ViewState["id"]) + " ");
                txtTitle.Text = dt.Rows[0]["Title"].ToString();
                ddlNewsType.SelectedValue = dt.Rows[0]["NewsType"].ToString();
                txtFrom.Text = dt.Rows[0]["NewsFrom"].ToString();
                txtAuthor.Text = dt.Rows[0]["Author"].ToString();
                txtNewsContent.Text = dt.Rows[0]["NewsContent"].ToString();
            }
           

        }

        //绑定新闻类别
        protected void ddlNewsTypeBinds()
        {
            ddlNewsType.DataSource=DBHelper.GetDataTable("select * from newsType");
            ddlNewsType.DataTextField = "Type";
            ddlNewsType.DataValueField = "ID";
            ddlNewsType.DataBind();
        }

        //保存
        protected void lbnSave_Click(object sender, EventArgs e)
        {
            if ( ViewState["id"] ==null)
            {
                string sql = " insert into News(Title,NewsFrom,NewsImage,Author,NewsContent,NewsType,UserID,CreateTime) "
           + "  Values(@Title,@NewsFrom,@NewsImage,@Author,@NewsContent,@NewsType,@UserID,@CreateTime)";
                SqlParameter[] parameter = new SqlParameter[] { 
            new SqlParameter("@Title",txtTitle.Text),
            new SqlParameter("@NewsFrom",txtFrom.Text),
            new SqlParameter("@NewsImage","........"),
            new SqlParameter("@Author",txtAuthor.Text),
            new SqlParameter("@NewsContent",txtNewsContent.Text),
            new SqlParameter("@NewsType",ddlNewsType.SelectedValue),
            new SqlParameter("@UserID",1),
            new SqlParameter("@CreateTime",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"))
            };
                int resule = DBHelper.ExecuteCommand(sql, parameter);
                if (resule > 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('保存成功')", true);

                }
            }

            else
            {
                string sql = "update News set ,"
                +" where ";
                SqlParameter[] parameter = new SqlParameter[]{
                 new SqlParameter("@Title",txtTitle.Text),
                  new SqlParameter("@NewsType",ddlNewsType.Text),
                   new SqlParameter("@NewsFrom",txtFrom.Text),
                    new SqlParameter("@Author",txtAuthor.Text),
                    new SqlParameter("@NewsContent",txtNewsContent.Text),
                    new SqlParameter("@id",ViewState["id"])
                };
                int result = DBHelper.ExecuteCommand(sql, parameter);
                if (result>0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),
                        "msg", "alert('更新成功'); location.href='NewsList.aspx'", true);
                }

            }
           


        }


        //取消
        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx");
        }
    }
}
通用后台(三)
通用后台(3)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace AfterEndDemo.Admin
{
    public partial class NewsTypeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GvNewsTypeListBinds();
            }
        }
        protected void GvNewsTypeListBinds()
        {
            gvNewsList.DataSource = DBHelper.GetDataTable("select* from NewsType");
            gvNewsList.DataBind();
        }

        protected void imbSearch_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvNewsList.EditIndex = e.NewEditIndex;
            this.GvNewsTypeListBinds();
        }

        protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvNewsList.EditIndex = -1;
            this.GvNewsTypeListBinds();
        }

        protected void gvNewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtType = gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;

            int id = Convert.ToInt32(gvNewsList.DataKeys[e.RowIndex].Value);
            string sql = "update NewsType set where ";
            DBHelper.ExecuteCommand(sql, new SqlParameter("Type", txtType.Text), new SqlParameter("@id", id));
            //
            gvNewsList.EditIndex = -1;
            this.GvNewsTypeListBinds();

        }
    }
}

 

posted @ 2012-06-25 11:00  ComBat  阅读(268)  评论(0编辑  收藏  举报