• 新闻发布系统 —— 练习记录


    公共类:

    CommonClass.cs

    View Code
    public class CommonClass
    {
    ///<summary>
    /// 连接数据库
    ///</summary>
    ///<returns>Sqlconnection对象</returns>
    public SqlConnection GetConnection()
    {
    string str = ConfigurationManager.AppSettings["Connection"].ToString();
    SqlConnection conn = new SqlConnection(str);
    return conn;
    }

    ///<summary>
    /// 在客户端弹出对话框
    ///</summary>
    ///<param name="TxtMessage">对话框显示的内容</param>
    ///<param name="Url">对话框关闭后跳转的URL</param>
    ///<returns></returns>
    public string MessageBox(string TxtMessage, string Url)
    {
    string str;
    str = "<script language=javascript>alert('" + TxtMessage + "');location='" + Url + "'</script>";
    return str;
    }

    ///<summary>
    ///执行sql
    ///</summary>
    ///<param name="sqlStr">SQL字符串</param>
    ///<returns>操作是否成功(true/false)</returns>
    public Boolean ExecSQL(string sqlStr)
    {
    SqlConnection conn = GetConnection();
    conn.Open();
    SqlCommand cmd = new SqlCommand(sqlStr, conn);
    try
    {
    cmd.ExecuteNonQuery();
    conn.Close();
    }
    catch (Exception)
    {

    conn.Close();
    return false;
    }
    return true;
    }

    ///<summary>
    /// 返回数据源的数据集
    ///</summary>
    ///<param name="sqlStr"></param>
    ///<param name="TableName"></param>
    ///<returns>数据集DataSet</returns>
    public DataSet GetDataset(string sqlStr, string TableName)
    {
    SqlConnection conn = GetConnection();
    conn.Open();
    SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, conn);
    DataSet ds = new DataSet();
    adapter.Fill(ds, TableName);
    conn.Close();
    return ds;
    }

    ///<summary>
    /// 防止SQL注入
    ///</summary>
    ///<param name="LoginName">登录名</param>
    ///<param name="loginPwd">登录密码</param>
    ///<returns></returns>
    public int check_Login(string loginName, string loginPwd)
    {
    SqlConnection conn = GetConnection();
    SqlCommand cmd = new SqlCommand("select count(*) from tb_User where Name=@loginName and PassWord=@loginPwd", conn);
    cmd.Parameters.Add(new SqlParameter("@loginName", SqlDbType.VarChar, 20));
    cmd.Parameters["@loginName"].Value = loginName;
    cmd.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.VarChar, 50));
    cmd.Parameters["@loginPwd"].Value = loginPwd;
    conn.Open();
    int i = (int)cmd.ExecuteScalar();
    cmd.Dispose();
    conn.Close();
    return i;
    }


    ///<summary>
    /// 实现随机验证码
    ///</summary>
    ///<param name="n">显示验证码的个数</param>
    ///<returns>返回生成的随机数</returns>
    public string RandomNum(int n)
    {
    //定义一个包括数字,大小写英文的字符串
    string strchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
    //将strchar字符串转化为数组
    //string.Split方法返回包含此实例的子字符串(由指定char数组的元素隔离)的string数组
    string[] vcArray = strchar.Split(',');
    string vNum = "";
    //记录上次随机数值,尽量避免几个一样的随机数
    int temp = -1;
    //采用一个简单的算法保证生成随机数不同
    Random rand = new Random();
    for (int i = 1; i < n + 1; i++)
    {
    if (temp != -1)
    {
    //unchecked 关键字用于取消整数运算和转换的溢出检查
    //DataTime.Ticks 属性火区表示此实例的日期和事件的刻度数
    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
    }

    //Random.Next 方法返回一个小于所指定最大值的非负随机数
    int t = rand.Next(35);
    if (temp != -1 && temp == t)
    {
    return RandomNum(n);
    }
    temp = t;
    vNum += vcArray[t];

    }
    return vNum;//返回生成的随机数
    }
    }


     

    Add.aspx 添加信息

    添加按钮

    protected void btnAdd_Click(object sender, EventArgs e)

    {

    cc.ExecSQL("insert into tb_News(Title,Content,Style,Type,IssueDate) values ('" + this.txtNewsTitle.Text.Trim() + "','" + this.txtNewsContent.Text.Trim() + "','" + this.labTitle.Text.Trim() + "','" + this.ddlNewsType.SelectedIndex.ToString() + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')");

    Response.Write(cc.MessageBox("添加成功!",""));

    }

    List.aspx 管理页面(搜索、删除)

    public partial class Manager_list : System.Web.UI.Page

    {

    CommonClass cc = new CommonClass();

    public static int InSearch;

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    //页面初始化时,指定IntSearch=0

    InSearch = 0;

    int n = Convert.ToInt32(Request.QueryString["id"]);

    //指定新闻类别

    this.dllNewsStyle.SelectedIndex = (n - 1);

    this.bind();

    }

    }

    public void bind()

    {

    this.gvNewsList.DataSource = cc.GetDataset("select * from tb_News where style='" + this.dllNewsStyle.SelectedValue.Trim() + "' order by id", "tbNews");

    this.gvNewsList.DataKeyNames = new string[] { "id" };

    this.gvNewsList.DataBind();

    }

    protected void btnSearch_Click(object sender, EventArgs e)

    {

    InSearch = 1;

    this.searchBind();

    }

    public void searchBind()

    {

    //使用like定义字符串

    string strSql = "select * from tb_News where style='" + this.dllNewsStyle.SelectedValue.ToString() + "'";

    strSql += " and (content like '%" + this.txtKey.Text + "%')";

    strSql += " or (Title like '%" + this.txtKey.Text + "%')";

    strSql += " and style='" + this.dllNewsStyle.SelectedValue.ToString() + "'";

    gvNewsList.DataSource = cc.GetDataset(strSql, "tbNews");

    this.gvNewsList.DataKeyNames = new string[] { "id" };

    gvNewsList.DataBind();

    }

    //实现 删除功能

    protected void gvNewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

    cc.ExecSQL("delete from tb_News where id='" + this.gvNewsList.DataKeys[e.RowIndex].Value.ToString() + "'");

    if (InSearch == 1)

    {

    this.searchBind();

    }

    else

    {

    bind();

    }

    }

    //删除 提示

    protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    e.Row.Cells[5].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");

    }

    }

    //实现 分页

    protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

    this.gvNewsList.PageIndex = e.NewPageIndex;

    if (InSearch == 1)

    {

    this.searchBind();

    }

    else

    {

    bind();

    }

    }

    }


    Menu.aspx控件

    前台搜索按钮

    protected void Page_Load(object sender, EventArgs e)

    {

    this.labDate.Text = System.DateTime.Now.ToString("yyyy年MM月dd日") + "" + DateTime.Now.DayOfWeek.ToString();

    }

    protected void btnSearch_Click(object sender, EventArgs e)

    {

    Session["tool"] = "新闻 ——> 站内查询(" + this.ddlStyle.SelectedValue.Trim() + ")----输入的关键字为“" + this.txtKey.Text.Trim() + "";

    string strSql = "select * from tb_News where style='" + this.ddlStyle.SelectedValue.ToString() + "'";

    //strSql += " and (content like '%" + this.txtKey.Text + "%')";

    strSql += " and Title like '%" + this.txtKey.Text + "%'";

    //strSql += " or issueDate='" + DateTime.Today.ToString() + "'";

    Session["search"] = strSql;

    Response.Redirect("search.aspx");

    //Response.Write(Session["search"].ToString());

    }

    Newslist.aspx

    新闻列表页

    根据值 选择 前台显示
    clip_image002

    当前页码为:

    [<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>]&nbsp;&nbsp; 总页码为:[<asp:Label

    ID="labBackPage" runat="server" Text=""></asp:Label>]

    <br />

    <asp:LinkButton ID="lnktnOne" runat="server" ForeColor="Red"

    onclick
    ="lnktnOne_Click">第一页</asp:LinkButton>

    &nbsp;<asp:LinkButton ID="lnktnUp" runat="server" ForeColor="Red"

    onclick
    ="lnktnUp_Click">上一页</asp:LinkButton>

    &nbsp;<asp:LinkButton ID="lnktnDown" runat="server" ForeColor="Red"

    onclick
    ="lnktnDown_Click">下一页</asp:LinkButton>

    &nbsp;<asp:LinkButton ID="lnktnBack" runat="server" ForeColor="Red"

    onclick
    ="lnktnBack_Click">最后一页</asp:LinkButton>


     

    Newslist.aspx.cs

    DataList 分页

    View Code
    CommonClass cc = new CommonClass();

    protected void Page_Load(object sender, EventArgs e)

    {

    bind();

    }

    string strStyle;

    public void bind()

    {

    int n = Convert.ToInt32(Request.QueryString["id"]);

    switch (n)

    {

    case 1: strStyle = "时政要闻";

    this.Label1.Text = "新闻网络中心 —> 时政要闻";

    this.imageLag.ImageUrl = "~/images/图片时政要闻.jpg";

    break;

    case 2: strStyle = "经济动向";

    this.Label1.Text = "新闻网络中心 —> 经济动向";

    break;

    default:

    break;

    }

    int curPage = int.Parse(this.labPage.Text);

    PagedDataSource ps = new PagedDataSource();

    DataSet ds = cc.GetDataset("select * from tb_News where style='" + strStyle + "' order by issueDate Desc", "tbNews");

    ps.DataSource = ds.Tables["tbNews"].DefaultView;

    //是否分页

    ps.AllowPaging = true;

    //分页数量

    ps.PageSize = 12;

    //取得当前页的页码

    ps.CurrentPageIndex = curPage - 1;

    this.lnktnBack.Enabled = true;

    this.lnktnDown.Enabled = true;

    this.lnktnOne.Enabled = true;

    this.lnktnUp.Enabled = true;

    if (curPage ==1)

    {

    //不显示第一页

    this.lnktnOne.Enabled = false;

    this.lnktnUp.Enabled = false;

    }

    if (curPage == ps.PageCount)

    {

    //不显示下一页

    this.lnktnDown.Enabled = false;

    this.lnktnBack.Enabled = false;

    }

    //显示分页数量

    this.labBackPage.Text = Convert.ToString(ps.PageCount);

    //绑定DataList

    this.DataList1.DataSource = ps;

    this.DataList1.DataKeyField = "id";

    this.DataList1.DataBind();

    }

    protected void lnktnOne_Click(object sender, EventArgs e)

    {

    this.labPage.Text = "1";

    this.bind();

    }

    protected void lnktnUp_Click(object sender, EventArgs e)

    {

    this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1);

    this.bind();

    }

    protected void lnktnDown_Click(object sender, EventArgs e)

    {

    this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1);

    this.bind();

    }

    protected void lnktnBack_Click(object sender, EventArgs e)

    {

    this.labPage.Text = this.labBackPage.Text;

    this.bind();

    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

    {

    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());

    //Response.Write("<script lanuage=javascript>window.open('showNews.aspx?id=" + id + "','','width=520,height=250')</script>");

    Server.Transfer("shownews.aspx?id='" + id + "'");

    Shownews.aspx.cs

    新闻内容页

    View Code
    CommonClass CC = new CommonClass();

    protected void Page_Load(object sender, EventArgs e)

    {

    DataSet ds = CC.GetDataset("select * from tb_News", "tbNews");

    DataRow[] row = ds.Tables["tbNews"].Select("id =" + Request.QueryString["id"] + "");

    foreach (DataRow rs in row)

    {

    this.Page.Title = rs["title"].ToString();

    this.labTitle.Text = rs["title"].ToString();

    this.txtContent.Text = "" + rs["content"].ToString();

    }

    }

    protected void btnClose_Click(object sender, EventArgs e)

    {

    Response.Write("<script language=javascript>window.close()</script>");

    }

    Search.aspx

    //新闻列表的ID

    //int id = Convert.ToInt32(dlNews.DataKeys[e.Item.ItemIndex].ToString());

    CommonClass cc = new CommonClass();

    protected void Page_Load(object sender, EventArgs e)

    {

    this.dlNews.DataSource = cc.GetDataset(Convert.ToString(Session["search"]), "tbNews");

    this.dlNews.DataKeyField = "id";

    this.dlNews.DataBind();

    //显示查询条件

    this.Label1.Text = Convert.ToString(Session["tool"]);

    }

    protected void dlNews_ItemCommand(object source, DataListCommandEventArgs e)

    {

    int id = Convert.ToInt32(dlNews.DataKeys[e.Item.ItemIndex].ToString());

    Response.Write("<script language=javascript>window.open('shownews.aspx?id=" + id + "','','width=520,height=260')</script>");

    }
  • 相关阅读:
    iOS 网络编程:socket
    Zsh和Bash,究竟有何不同
    关于微软win10 2004的更新以及wsl2 Ubuntu18.04安装
    C# 委托事件机制 订阅发布
    关于Docker理念和安装,对Visual Studio2019自带生成的DockerFile配置,以及Docker镜像的发布与拉取
    div弹框
    vs分页
    ado显示下拉
    SQL server动态拼接存储过程分页
    退役
  • 原文地址:https://www.cnblogs.com/tangge/p/2220168.html
Copyright © 2020-2023  润新知