获取contentEditable标记的重写值

时间:2012-11-27 06:35:53

标签: c# asp.net html5

在我的代码中我想设置一个带有.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;
    }
}

0 个答案:

没有答案