⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx.cs

📁 具体功能质量高得到以后可以到我的博客去看看你能收获很多
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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 guihua.App_Code;
public partial class _Default : System.Web.UI.Page 
{
    guihua.App_Code.LeaveMessagePara LMP = new LeaveMessagePara();
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    DataSet ds1 = new DataSet();
    DataSet ds2 = new DataSet();
    DataSet ds3 = new DataSet();
    DataSet ds4 = new DataSet();
    DataSet ds5 = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        //禁止浏览器的缓存,防止用户发布信息后点“后退”返回到原来未发布的状态(虽然实际数据已写入DB)
        Response.Cache.SetNoStore();
        Response.Cache.SetNoServerCaching();

        Response.Write("<script type=\"text/javascript\">");
        Response.Write("function CtrlEnter(button){");
        Response.Write("if(event.ctrlKey && event.keyCode==13)document.all[button].click();}");
        Response.Write("var floor=" + Application["TableCount"] + ";");
        Response.Write("function NextFloor(){floor--;}");
        Response.Write("</script>");

        databing();
       // ds2 = LMManage.GetLeaveMessage1(0);
       // Repeater1.Visible = true;
       // Repeater1.
       // Repeater1.DataSource = ds2;
       // Repeater1.DataBind();

        ds4 = LMManage.GetLeaveMessage1(0);
        DataList1.DataSource = ds4;
        DataList1.DataBind();

        DetailsView1.DataSource = ds4;
        DetailsView1.DataBind();

        //ds3 = LMManage.GetLeaveMessage1(0);
        //DataGrid1.DataSource = ds3;
        //DataGrid1.DataBind();
       
        //ds = LMManage.GetLeaveMessage1(0);
        //GridView1.DataSource = ds;
        //GridView1.DataBind();

        //dr = LMManage.GetLeaveMessage(0);
        //ListBox1.DataSource = dr;
        //ListBox1.DataTextField = "Contents";
        //ListBox1.DataValueField = "ID";
        //ListBox1.DataBind();
        //dr.Close();
        
        //dr = LMManage.GetLeaveMessage(0);
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
      

       if (!Page.IsPostBack)
        {
            Response.Write("<Script Language=JavaScript>alert(\"代码执行了!IsPostBack。\")</Script>");
            ds = LMManage.GetLeaveMessage1(0);
            GridView1.DataSource = ds;
            GridView1.DataBind();

            ds3 = LMManage.GetLeaveMessage1(0);
            DataGrid1.DataSource = ds3;
            DataGrid1.DataBind();

        }
        if (!Page.IsPostBack)
        {
            dr = LMManage.GetLeaveMessage(0);
            DropDownList1.DataSource = dr;
            DropDownList1.DataTextField = "Title";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            //dr.Close();
            this.DropDownList1.SelectedIndex = 0;
            TBcontents.Attributes.Add("onkeydown", "CtrlEnter('Submit');");
           
           // if (Session["BUser"] != null)
           // {
           //     TBuser.Text = Session["BUser"].ToString();
                //Session["BUser"] = null;
            //}
            //if (Session["QQorMSN"] != null)
            //{
              //  TBQQMSN.Text = Session["QQorMSN"].ToString();
                //Session["QQorMSN"] = null;
            //}
        }
    }
  //  protected void subListChange(object s, EventArgs e)
  //  {
  //      string GetLM = "";
  //      string dd = this.ListBox1.SelectedValue;
  //      GetLM = "SELECT * FROM LM01 WHERE ID=" +this.ListBox1.SelectedItem.Value.ToString();
        
  //      ds1 = DBControl.DBSelect1(GetLM);
  //      GridView2.DataSource = ds1;
  //      GridView2.DataBind();
  //  }
    protected void Title_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TBcontents_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (TBtitle.Text == "" || TBcontents.Text == "" )
        {
            Response.Write("<Script Language=JavaScript>alert(\"提示:标题、内容项必填。\")</Script>");
            Response.Write("<Script Language=JavaScript>this.location.href=\"#IwantToSay\"</script>");

            Response.Write("<script>setTimeout(\"document.all.TBtitle.focus();\",500);</script>");
            //上行代码也可写成两段
            //Response.Write("<script>setTimeout(\"Focus()\",1000);</script>");
            //Response.Write("<script>function Focus(){document.all.TBuser.focus();}</script>");

            //TBuser.Focus();   //用这个在调试时总是提示内存不可读
        }
        else
        {
            LMP.ID = AutoCreateID.NewID();
            //LMP.User = FormatString.FormatStr(TBuser.Text);
            //LMP.QQMSN = FormatString.FormatStr(TBQQMSN.Text);
            LMP.Title = FormatString.FormatStr(TBtitle.Text);
            LMP.Contents = FormatString.FormatStr(TBcontents.Text);
            LMP.WriteBack = "";
            LMP.SubmitTime = DateTime.Now;

            if (LMManage.AddNewLeaveMessage(LMP))
            {
                //每次向数据库写入一条留言成功,就使保存 表行数 的全局变量 +1
               // Application.Lock();
               // Application["TableCount"] = (int)Application["TableCount"] + 1;
               // Application.UnLock();
                
                TBtitle.Text = "";
                TBcontents.Text = "";
               
           //     LMBind();

                //采用网页重定向的方法,防止用户提交后刷新页面又提示再一次提交
                //(同时注意保留用户已填写的用户名和联系方式)
                //Session["BUser"] = LMP.User;
                //Session["QQorMSN"] = LMP.QQMSN;
                Response.Redirect("Default.aspx");                  //为解决刷新问题
               
            }
            //else Response.Redirect("ShowErrorMsg.aspx?name=" + LMP.User);
        }
    }
   // protected void LMBind()
   // {
       // if (!DBControl.AccessConnection)
   //         LeaveMessageList.DataSource = LMManage.GetLeaveMessage(10);
       // else
        //    LeaveMessageList.DataSource = LMManage.aGetLeaveMessage(10);
   //     LeaveMessageList.DataBind();
 //  }
    protected string ShortString(string content, string id)
    {
        //限制显示“留言内容”的字符数,避免篇幅太大
        if (content.Length > 180)
            return content.Substring(0, 150) + "...&nbsp;&nbsp;" + "<a href=\"ShowDetail.aspx?d_ID=" + id + "\">阅读全文</a>";
        else
            return content;

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       // int x = this.DropDownList1.SelectedIndex;
        string GetLM = "";
        //string dd = this.ListBox1.SelectedValue;
        //this.Label2.Text = this.DropDownList1.SelectedItem.Value.ToString();
        GetLM = "SELECT * FROM LM01 WHERE ID=" + "'" + this.DropDownList1.SelectedItem.Value.ToString()/*或者可以用DropDownList1.SelectedValue*/ + "'";
        ds1 = DBControl.DBSelect1(GetLM);
        GridView2.DataSource = ds1;
        GridView2.DataBind();
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ds = LMManage.GetLeaveMessage1(0);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    private void databing()
    {
        ds2 = LMManage.GetLeaveMessage1(0);

        PagedDataSource pagesource = new PagedDataSource();
        
        pagesource.DataSource = ds2.Tables[0].DefaultView;
        
        pagesource.AllowPaging = true;
        pagesource.PageSize = 5;
        int CurPage;
        if (Request.QueryString["Page"] != null)
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            CurPage = 1;
 
        pagesource.CurrentPageIndex = CurPage - 1;
        this.Label1.Text = "当前页:" + CurPage.ToString();

        if (!pagesource.IsFirstPage)
            this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

        if (!pagesource.IsLastPage)
            this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);

        Repeater1.DataSource = pagesource;
        //try
        //{
            Repeater1.DataBind();
        //}
        //catch ( NullReferenceException e){
        //    Response.Write("wrong2");
        //}
            //DataList1.DataSource = pagesource;
            //DataList1.DataBind();
        this.Label2.Text = "总页数"+pagesource.PageCount.ToString();
    }

    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        ds5 = LMManage.GetLeaveMessage1(0);
        DetailsView1.DataSource = ds5;
        DetailsView1.DataBind();
    }
    protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        ds3 = LMManage.GetLeaveMessage1(0);
        DataGrid1.DataSource = ds3;
        DataGrid1.DataBind();
    }
    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = -1;
        ds3 = LMManage.GetLeaveMessage1(0);
        DataGrid1.DataSource = ds3;
        DataGrid1.DataBind();
    }
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string id = ((TextBox)(DataGrid1.Items[e.Item.ItemIndex].Cells[0].Controls[0])).Text.ToString().Trim();
        //取得編輯行的關鍵字段的值
        //string empid =(string) this.DataGrid1.DataKeys[e.Item.ItemIndex];
        //取得文本框中輸入的內容,如果使用數據綁定列((TextBox)e.Item.Cells[2].Controls[0].Text方式取得
        string newtext = ((TextBox)(DataGrid1.Items[e.Item.ItemIndex].Cells[1].Controls[0])).Text.ToString().Trim();
      
        string sql = "update LM01 set Title='" + newtext + "' where id='" + id + "'";
        
       try
       {
           this.DataGrid1.EditItemIndex = -1;

            DBControl.DBExcuteIUD(sql);
            ds3 = LMManage.GetLeaveMessage1(0);
            DataGrid1.DataSource = ds3;
            DataGrid1.DataBind();
            
            
        }

        catch (Exception err)
        {
            Response.Write(err.ToString());
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        ds = LMManage.GetLeaveMessage1(0);
        GridView1.DataSource = ds;
        GridView1.DataBind();
       // Response.Write("<Script Language=JavaScript>alert(\"点击了编辑按钮\")</Script> ");
        
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        ds = LMManage.GetLeaveMessage1(0);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    //protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    //{
    //   // string id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
    //   // string title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
    //   // string title2 = (string)e.NewValues[1].ToString();
    //    string id = "";
    //    string title = "";
    //    string strSql = "update lm01 set title='" + title + "'" + "where id='" + id + "';";
    //    int num = DBControl.DBExcuteIUD(strSql);
    //    GridView1.EditIndex = -1;
    //    ds = LMManage.GetLeaveMessage1(0);
    //    GridView1.DataSource = ds;
    //    GridView1.DataBind();
    //}
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //string id = e.NewValues["id"].ToString();
        string id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        //string title2 = (string)e.NewValues[e.RowIndex].ToString();
        string strSql = "update lm01 set title='" + title + "'" + "where id='" + id + "';";
        int num = DBControl.DBExcuteIUD(strSql);
        GridView1.EditIndex = -1;
        ds = LMManage.GetLeaveMessage1(0);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -