admin.aspx.cs

来自「数据库连接查询」· CS 代码 · 共 74 行

CS
74
字号
using System;
using System.Data;
using System.Configuration;
using System.Linq;
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 System.Xml.Linq;
using System.Data.Linq;
using System.IO;

public partial class Admin : System.Web.UI.Page
{
    GuestBookDataContext ctx = new GuestBookDataContext("server=srv-devdbhost;database=GuestBook;uid=sa;pwd=Abcd1234");
        
    protected void Page_Load(object sender, EventArgs e)
    {
       

        //StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), false);
        //ctx.Log = sw;
        //var messages = from gb in ctx.tbGuestBooks
        //               select gb;
        //foreach (var gb in messages)
        //{
        //    if(gb.UserName == "admin")
        //        gb.UserName = "notadmin";
        //}

        //ctx.SubmitChanges();

        if (!IsPostBack)
        {
            SetBind();
        }

        //sw.Close();
    }
 
    private void SetBind()
    {
        rpt_Message.DataSource = from gb in ctx.tbGuestBooks orderby gb.PostTime descending select gb;
        rpt_Message.DataBind();
    }
    protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "DeleteMessage")
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true); // Append
            ctx.Log = sw;

            tbGuestBook gb = ctx.tbGuestBooks.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));
            ctx.tbGuestBooks.Remove(gb);
            ctx.SubmitChanges();
            SetBind();
            sw.Close();
        }
        if (e.CommandName == "SendReply")
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), false); // Append
            ctx.Log = sw;
            tbGuestBook gb = ctx.tbGuestBooks.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));
            gb.Reply = ((TextBox)e.Item.FindControl("tb_Reply")).Text;
            gb.IsReplied = true;
            ctx.SubmitChanges();
            SetBind();
            sw.Close();
        }
    }
}

⌨️ 快捷键说明

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