1

在我的代码中,我想使用来自 .mdb 数据库(oleDb)的值设置一个标签,当用户注册为管理员时,我将 contentEditable=true 属性添加到我希望他能够编辑的部分。完成后,他将点击“保存”按钮,后面的代码会将新值更新到数据库中。
这是代码:(Admin.LogIn(,); 如果用户注册为管理员,也返回 true)

  <%@ Page Title="" Language="C#" MasterPageFile="~/BenOptic.master" AutoEventWireup="true" CodeFile="ben_info.aspx.cs" Inherits="ben_info" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1 align="center" id="ben_info_header" runat="server">

</h1>
<h3 align="center" id="ben_info_subheader" runat="server">

</h3>
<p id="ben_info_main_text" runat="server">

</p>
<form id="Form1" action='' method='post' runat='server'><asp:Button id='SaveChanges' Text='שמור' OnClick='saveChanges_Click' runat='server' /></form>

</asp:Content>

以及背后的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class ben_info : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SaveChanges.Visible = false;
        DAL data = new DAL();
        if (Request.Form["submit"] != null)
        {
            if (Admin.LogIn(Request.Form["adm_username"], Request.Form["adm_pass"]))
            {
                ben_info_header.Attributes.Add("contenteditable", Session["isAdmin"].ToString());
                ben_info_main_text.Attributes.Add("contenteditable", Session["isAdmin"].ToString());
                ben_info_subheader.Attributes.Add("contenteditable", Session["isAdmin"].ToString());
                SaveChanges.Visible = true;
                SaveChanges.OnClientClick += new EventHandler(saveChanges_Click);
            }
        }


        DataTable config = data.Select("Select *  from Config where setting like 'ben_info%'");
        foreach (DataRow setting in config.Rows)
        {
            switch (setting["setting"].ToString())
            {
                case "ben_info_header": ben_info_header.InnerText = setting["val"].ToString(); break;
                case "ben_info_subheader": ben_info_subheader.InnerText = setting["val"].ToString(); break;
                case "ben_info_main_text": ben_info_main_text.InnerText = setting["val"].ToString(); break;
            }
        }
    }

    public void saveChanges_Click(object sender, EventArgs e)
    {
        DAL data = new DAL();
        data.Execute("update Config set val = '" + ben_info_header.InnerText + "' where setting = 'ben_info_header'");
        data.Execute("update Config set val = '" + ben_info_subheader.InnerText + "' where setting = 'ben_info_subheader'");
        data.Execute("update Config set val = '" + ben_info_main_text.InnerText + "' where setting = 'ben_info_main_text'");
        SaveChanges.Visible = false;
    }
}
4

0 回答 0