.NET开发笔记--对config文件的操作(1)

1先写一些常用的公共类:

在Web.config文件中的配置:

<!-- appSettings网站信息配置-->
  <appSettings>
    <add key="Configpath" value="~/xmlconfig/site.config"/>
    <add key="Urlspath" value="~/xmlconfig/urls.config"/>
    <add key="Userpath" value="~/xmlconfig/user.config"/>
  </appSettings>
  <!-- 数据库连接字符串-->
  <connectionStrings>
    <add name="ConnectionString" connectionString="server=.\;uid=sa;pwd=123;database=Test;"/>
  </connectionStrings>

2、在DbHelperSQL.cs中写基本的对数据库操作的方法

  public abstract class DbHelperSQL
    {
         //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.        
        public static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        public DbHelperSQL(){ }

/// <summary>
        /// 执行查询语句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }

        }

#region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句,返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        throw e;
                    }
                }
            }
        }

    /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_sys_channel ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

}

3、在后台代码中进行调用

        /// <summary>
    /// 数据访问类:系统频道
    /// </summary>
    public partial class sys_channel
    {
        public sys_channel()
        {}
                            /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DTcms.Model.sys_channel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_sys_channel set ");
            strSql.Append("model_id=@model_id,");
            strSql.Append("name=@name,");
            strSql.Append("title=@title,");
            strSql.Append("sort_id=@sort_id");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@model_id", SqlDbType.Int,4),
                    new SqlParameter("@name", SqlDbType.NVarChar,100),
                    new SqlParameter("@title", SqlDbType.NVarChar,100),
                    new SqlParameter("@sort_id", SqlDbType.Int,4),
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.model_id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.title;
            parameters[3].Value = model.sort_id;
            parameters[4].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_sys_channel ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DTcms.Model.sys_channel GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,model_id,name,title,sort_id from dt_sys_channel ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DTcms.Model.sys_channel model = new DTcms.Model.sys_channel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["model_id"] != null && ds.Tables[0].Rows[0]["model_id"].ToString() != "")
                {
                    model.model_id = int.Parse(ds.Tables[0].Rows[0]["model_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "")
                {
                    model.title = ds.Tables[0].Rows[0]["title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sort_id"] != null && ds.Tables[0].Rows[0]["sort_id"].ToString() != "")
                {
                    model.sort_id = int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,model_id,name,title,sort_id,model_title ");
            strSql.Append(" FROM view_sys_channel ");
            if(strWhere.Trim()!="")
            {
                strSql.Append(" where "+strWhere);
            }
            strSql.Append(" order by sort_id asc,id desc");
            return DbHelperSQL.Query(strSql.ToString());
        }


             }                   

4、后台代码

public partial class sys_channel
    {
        private readonly DTcms.DAL.sys_channel dal = new DTcms.DAL.sys_channel();
        public sys_channel()
        {}

/// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DTcms.Model.sys_channel model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DTcms.Model.sys_channel model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DTcms.Model.sys_channel GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

}

5、页面后台代码

public partial class sys_channel_edit : DTcms.Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg("传输参数不正确!", "back", "Error");
                    return;
                }
                if (!new DTcms.BLL.sys_channel().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除!", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                TreeBind(this.ddlModelId); //绑定模型
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定模型=================================
        private void TreeBind(DropDownList ddl)
        {
            DTcms.BLL.sys_model bll = new DTcms.BLL.sys_model();
            DataTable dt = bll.GetList("").Tables[0];

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem("请选择模型", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sys_channel bll = new BLL.sys_channel();
            Model.sys_channel model = bll.GetModel(_id);
            txtName.Text = model.name;
            hidName.Value = model.name;
            txtTitle.Text = model.title;
            ddlModelId.SelectedValue = model.model_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            //绑定URL配置列表
            rptList.DataSource = new BLL.url_rewrite().GetList(model.id.ToString());
            rptList.DataBind();
        }
        #endregion

        #region 根据类型返回文件名========================
        //private string GetPageName(string _name, string page_type)
        //{
        //    string result = "";
        //    switch (page_type)
        //    {
        //        case "index":
        //            result = _name + ".aspx";
        //            break;
        //        case "list":
        //            result = _name + "_list.aspx";
        //            break;
        //        case "detail":
        //            result = _name + "_show.aspx";
        //            break;
        //    }
        //    return result;
        //}
        #endregion

        #region 返回页面的类型============================
        protected string GetPageTypeName(string _type_name)
        {
            string result = "";
            switch (_type_name)
            {
                case "index":
                    result = "首页";
                    break;
                case "list":
                    result = "列表页";
                    break;
                case "detail":
                    result = "详细页";
                    break;
            }
            return result;
        }
        #endregion

        #region 查询频道的页面继承类======================
        private string GetInherit(int model_id, string page_type)
        {
            string result = "";
            BLL.sys_model bll = new BLL.sys_model();
            Model.sys_model model = bll.GetModel(model_id);
            if (model != null)
            {
                switch (page_type)
                {
                    case "index":
                        result = model.inherit_index;
                        break;
                    case "list":
                        result = model.inherit_list;
                        break;
                    case "detail":
                        result = model.inherit_detail;
                        break;
                }
            }
            return result;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.sys_channel model = new Model.sys_channel();
            BLL.sys_channel bll = new BLL.sys_channel();
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            int new_id = bll.Add(model); //保存
            if (new_id < 1)
            {
                return false;
            }
            #region 保存URL配置.开始===================================
            BLL.url_rewrite bll2 = new BLL.url_rewrite();
            bll2.Remove("channel", new_id.ToString()); //先删除
            string[] itemTypeArr = Request.Form.GetValues("item_type");
            string[] itemNameArr = Request.Form.GetValues("item_name");
            string[] itemPathArr = Request.Form.GetValues("item_path");
            string[] itemPatternArr = Request.Form.GetValues("item_pattern");
            string[] itemQuerystringArr = Request.Form.GetValues("item_querystring");
            string[] itemTempletArr = Request.Form.GetValues("item_templet");
            string[] itemPageArr = Request.Form.GetValues("item_page");
            if (itemTypeArr != null && itemNameArr != null && itemPathArr != null
                && itemPatternArr != null && itemQuerystringArr != null && itemTempletArr != null && itemPageArr != null)
            {
                if ((itemTypeArr.Length == itemNameArr.Length) && (itemNameArr.Length == itemPathArr.Length) && (itemPathArr.Length == itemPatternArr.Length)
                    && (itemPatternArr.Length == itemQuerystringArr.Length) && (itemQuerystringArr.Length == itemTempletArr.Length))
                {
                    for (int i = 0; i < itemTypeArr.Length; i++)
                    {
                        bll2.Add(new Model.url_rewrite
                        {
                            name = itemNameArr[i].Trim(),
                            path = itemPathArr[i].Trim(),
                            pattern = itemPatternArr[i].Trim(),
                            //page = GetPageName(model.name, itemTypeArr[i].Trim()), //源页面地址
                            page = itemPageArr[i].Trim(),
                            querystring = itemQuerystringArr[i].Trim(),
                            templet = itemTempletArr[i].Trim(),
                            channel = new_id.ToString(),
                            type = itemTypeArr[i].Trim(),
                            inherit = GetInherit(model.model_id, itemTypeArr[i].Trim()) //继承的类名
                        });
                    }
                }
            }
            #endregion 保存URL配置.结束

            return true;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            DTcms.BLL.sys_channel bll = new DTcms.BLL.sys_channel();
            DTcms.Model.sys_channel model = bll.GetModel(_id);
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            if (!bll.Update(model))
            {
                return false;
            }

            #region 保存URL配置.开始===================================
            BLL.url_rewrite bll2 = new BLL.url_rewrite();
            bll2.Remove("channel", model.id.ToString()); //先删除
            string[] itemTypeArr = Request.Form.GetValues("item_type");
            string[] itemNameArr = Request.Form.GetValues("item_name");
            string[] itemPathArr = Request.Form.GetValues("item_path");
            string[] itemPatternArr = Request.Form.GetValues("item_pattern");
            string[] itemQuerystringArr = Request.Form.GetValues("item_querystring");
            string[] itemTempletArr = Request.Form.GetValues("item_templet");
            string[] itemPageArr = Request.Form.GetValues("item_page");
            if (itemTypeArr != null && itemNameArr != null && itemPathArr != null
                && itemPatternArr != null && itemQuerystringArr != null && itemTempletArr != null)
            {
                if ((itemTypeArr.Length == itemNameArr.Length) && (itemNameArr.Length == itemPathArr.Length) && (itemPathArr.Length == itemPatternArr.Length)
                    && (itemPatternArr.Length == itemQuerystringArr.Length) && (itemQuerystringArr.Length == itemTempletArr.Length))
                {
                    for (int i = 0; i < itemTypeArr.Length; i++)
                    {
                        bll2.Add(new Model.url_rewrite
                        {
                            name = itemNameArr[i].Trim(),
                            path = itemPathArr[i].Trim(),
                            pattern = itemPatternArr[i].Trim(),
                            //page = GetPageName(model.name, itemTypeArr[i].Trim()), //源页面地址
                            page = itemPageArr[i].Trim(),
                            querystring = itemQuerystringArr[i].Trim(),
                            templet = itemTempletArr[i].Trim(),
                            channel = model.id.ToString(),
                            type = itemTypeArr[i].Trim(),
                            inherit = GetInherit(model.model_id, itemTypeArr[i].Trim()) //继承的类名
                        });
                    }
                }
            }
            #endregion 保存URL配置.结束

            return true;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("sys_channel", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦!", "", "Error");
                    return;
                }
                JscriptMsg("修改信息成功啦!", "sys_channel_list.aspx", "Success", "parent.loadChannelTree");
            }
            else //添加
            {
                ChkAdminLevel("sys_channel", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦!", "", "Error");
                    return;
                }
                JscriptMsg("添加信息成功啦!", "sys_channel_list.aspx", "Success", "parent.loadChannelTree");
            }
        }

    }

6、前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_channel_edit.aspx.cs" Inherits="DTcms.Web.admin.settings.sys_channel_edit" ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>编辑频道信息</title>
<link href="../../scripts/ui/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="../images/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../scripts/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../../scripts/jquery/jquery.validate.min.js"></script> 
<script type="text/javascript" src="../../scripts/jquery/messages_cn.js"></script>
<script type="text/javascript" src="../../scripts/ui/js/ligerBuild.min.js"></script>
<script type="text/javascript" src="../js/function.js"></script>
<script type="text/javascript">
    //表单验证
    $(function () {
        $('#txtName').focus();
        $("#form1").validate({
            errorPlacement: function (lable, element) {
                element.ligerTip({ content: lable.html(), appendIdTo: lable });
            },
            rules: {
                txtName: {
                    required: true,
                    minlength: 2,
                    maxlength: 100,
                    remote: {
                        type: "post",
                        url: "../../tools/admin_ajax.ashx?action=sys_channel_validate",
                        data: {
                            channelname: function () {
                                return $("#txtName").val();
                            },
                            oldname: function () {
                                return $("#hidName").val();
                            }
                        },
                        dataType: "html",
                        dataFilter: function (data, type) {
                            if (data == "true")
                                return true;
                            else
                                return false;
                        }
                    }
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
            },
            messages: {
                txtName: {
                    required: "输入(2-100)位字符",
                    minlength: "必须大于2位字符",
                    maxlength: "必须小于100位字符",
                    remote: "很抱歉,该名称已被使用"
                }
            }
        });
    });
    //菜单事件处理
    $(function () {
        //初始化按钮事件
        $("#item_box tr").each(function (i) {
            initButton(i);
        });
        //添加按钮(点击绑定)
        $("#itemAddButton").click(function () {
            showChannelDialog();
        });
    });

    //表格行的菜单内容
    function getTr() {
        var navRow = '<tr class="td_c">'
        + '<td><input type="hidden" name="item_type" /><input type="hidden" name="item_page" /><span class="item_type"></span></td>'
        + '<td><input name="item_name" type="text" class="txtInput small" style="width:98%;" readonly="readonly" /></td>'
        + '<td><input name="item_path" type="text" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>'
        + '<td><input name="item_pattern" type="text" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>'
        + '<td><input name="item_querystring" type="text" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>'
        + '<td><input name="item_templet" type="text" class="txtInput small" style="width:98%;" readonly="readonly" /></td>'
        + '<td align="center"><img alt="编辑" src="../images/icon_edit.gif" class="operator" /><img alt="删除" src="../images/icon_del.gif" class="operator" /></td>'
        + '</tr>';
        return navRow;
    }

    //初始化按钮事件
    function initButton(indexValue) {
        //功能操作按钮
        $("#item_box tr:eq(" + indexValue + ") .operator").each(function (i) {
            switch (i) {
                //修改
                case 0:
                    $(this).click(function () {
                        showChannelDialog(this);
                    });
                    break;
                //删除 
                case 1:
                    $(this).click(function () {
                        var obj = $(this);
                        $.ligerDialog.confirm("确定要删除吗?", "提示信息", function (result) {
                            if (result) {
                                obj.parent().parent().remove(); //删除节点
                            }
                        });
                    });
                    break;
            }
        });
    }

    //给URL配置首页赋值
    function itemSelect(obj) {
        var value = $(obj).children("option:selected").attr("value");
        if (value.length < 1) {
            return false;
        }
        var channelName = $("#txtName").val(); //获得频道名称
        if (channelName.length < 1) {
            return false;
        }
        //咨询是否使用默认设置
        $.ligerDialog.confirm("是否使用参考配置?做为参考,请根据自己实际情况调整。", "提示信息", function (result) {
            if (result) {
                switch (value) {
                    case "list":
                        $("#urlKey").val(channelName + "_list");
                        $("#urlExp").val(channelName + "/{0}/{1}.aspx");
                        $("#var_box").empty(); //清空旧数据
                        $("#var_box").append(createVarHtml()); //插入一行
                        $("#var_box tr:last").find("input[name='varName']").val("category_id");
                        $("#var_box tr:last").find("input[name='varExp']").val("(\\d+)");
                        $("#var_box").append(createVarHtml()); //插入一行
                        $("#var_box tr:last").find("input[name='varName']").val("page");
                        $("#var_box tr:last").find("input[name='varExp']").val("(\\w+)");
                        $("#pageTemplet").val(channelName + "_list");
                        $("#urlPage").val(channelName + "_list");
                        break;
                    case "detail":
                        $("#urlKey").val(channelName + "_show");
                        $("#urlExp").val(channelName + "/show/{0}.aspx");
                        $("#var_box").empty(); //清空旧数据
                        $("#var_box").append(createVarHtml()); //插入一行
                        $("#var_box tr:last").find("input[name='varName']").val("id");
                        $("#var_box tr:last").find("input[name='varExp']").val("(\\d+)");
                        $("#pageTemplet").val(channelName + "_show");
                        $("#urlPage").val(channelName + "_show");
                        break;
                    default:
                        $("#var_box").empty(); //清空旧数据
                        $("#urlKey").val(channelName);
                        $("#urlExp").val(channelName + ".aspx");
                        $("#pageTemplet").val(channelName);
                        $("#urlPage").val(channelName);
                        break;
                }
            }
        });
    }

    //===================================================================================
    //创建窗口
    function showChannelDialog(obj) {
        var objNum = arguments.length;
        var tabHtml = createChannelHtml();
        var m = $.ligerDialog.open({
            type: "",
            title: "URL重写配置",
            content: tabHtml,
            width: 700,
            buttons: [
            { text: '确认', onclick: function () {
                if (objNum > 0) {
                    execChannelDialog(m, obj);
                } else {
                    execChannelDialog(m);
                }
            }
            },
            { text: '关闭', onclick: function () {
                m.close();
            }
            }
            ],
            isResize: true
        });
        //检查是否修改状态
        if (objNum == 1) {
            //调用赋值函数
            setChannelDialog(obj);
        }
    }

    //创建HTML
    function createChannelHtml() {
        var tableHtml = '<table class="form_table">'
        + '<col width="130px"><col>'
        + '<tbody>'
        + '<tr>'
        + '<th>频道类型:</th>'
        + '<td><select id="pageType" class="select2" οnchange="itemSelect(this);"><option value="">请选择类型...</option><option value="index">首页</option><option value="list">列表页</option><option value="detail">详细页</option></select></td>'
        + '</tr><tr>'
        + '<th>URL名称(Key):</th>'
        + '<td><input type="text" id="urlKey" class="txtInput small ime-disabled" /><label>*调用的唯一标识,英文、数字、下划线</label></td>'
        + '</tr><tr>'
        + '<th>重写表达式:</th>'
        + '<td><input type="text" id="urlExp" class="txtInput small ime-disabled" style="width:210px;" /><label>*如:article-{0}-{1}.aspx,{n}表示第N个变量</label></td>'
        + '</tr><tr>'
        + '<th>传输参数:</th>'
        + '<td><button id="btnVarAdd" type="button" class="btnSelect" οnclick="addVarTr();"><span class="add">增加变量</span></button></td>'
        + '</tr><tr>'
        + '<th>&nbsp;</th>'
        + '<td><table border="0" cellspacing="0" cellpadding="0" class="border_table" width="98%">'
        + '<thead><tr><th width="40%">变量名称</th><th>正则表达式</th><th width="3%">操作</th></tr></thead>'
        + '<tbody id="var_box">'
        + '</tbody></table></td>'
        + '</tr><tr>'
        + '<th>模板文件:</th><td><input type="text" id="pageTemplet" value="" class="txtInput small ime-disabled" />.html</td>'
        + '</tr><tr>'
        + '<th>生成的文件:</th><td><input type="text" id="urlPage" value="" class="txtInput small ime-disabled" />.aspx</td>'
        + '</tr>'
        + '</tbody>'
        + '</table>'
        return tableHtml;
    }
    //创建URL变量HTML
    function createVarHtml() {
        varHtml = '<tr>'
        + '<td><select class="select2" οnchange="regChannelVal(this, \'varName\');"><option value="">@参考</option><option value="category_id">类别ID</option><option value="page">分页页码</option></select>'
        + '<input type="text" name="varName" class="txtInput small ime-disabled" /></td>'
        + '<td><select class="select2" οnchange="regChannelVal(this, \'varExp\');"><option value="">@参考</option><option value="(\\w+)">字符串</option><option value="(\\d+)">数字</option></select>'
        + '<input type="text" name="varExp" class="txtInput small ime-disabled" style="width:160px;" /></td>'
        + '<td><img alt="删除" src="../images/icon_del.gif" class="operator" οnclick="delVarTr(this);" /></td>'
        +'</tr>'
        return varHtml;
    }

    //添加一行变量
    function addVarTr() {
        varHtml = createVarHtml();
        $("#var_box").append(varHtml);
    }
    //删除一行变量
    function delVarTr(obj) {
        $(obj).parent().parent().remove();
    }

    //赋值参考选项
    function regChannelVal(obj, objName) {
        var value = $(obj).children("option:selected").attr("value");
        if (value != "") {
            $(obj).next("input[name='" + objName + "']").val(value);
        }
    }

    //赋值表单
    function setChannelDialog(obj) {
        var pobj = $(obj).parent().parent();
        var item_type = $(pobj).find("input[name='item_type']").val();
        $("#pageType option").each(function (i) {
            if ($(this).attr("value") == item_type) {
                $(this).attr("selected", "selected");
            }
        });
        $("#urlKey").val($(pobj).find("input[name='item_name']").val());
        $("#urlExp").val($(pobj).find("input[name='item_path']").val());
        $("#pageTemplet").val($(pobj).find("input[name='item_templet']").val().replace(".html", ""));
        $("#urlPage").val($(pobj).find("input[name='item_page']").val().replace(".aspx", ""));
        //分析正则表达式
        var strPath = $(pobj).find("input[name='item_path']").val().replace(new RegExp("{\\d+}", "g"), "(.*)"); //替换成正则表达式
        var strPattern = $(pobj).find("input[name='item_pattern']").val();
        var pathArr = strPattern.match(strPath);
        //开始赋值
        if ($(pobj).find("input[name='item_querystring']").val() != "") {
            var querystrArr = $(pobj).find("input[name='item_querystring']").val().split("^");
            for (i = 0; i < querystrArr.length; i++) {
                //插入一行TR并赋值变量
                var trObj = $("#var_box").append(createVarHtml());
                var strArr = querystrArr[i].split("=");
                $(trObj).children("tr").eq(i).find("input[name='varName']").val(strArr[0]);
                //赋值正则表达式
                $(trObj).children("tr").eq(i).find("input[name='varExp']").val(pathArr[i + 1]);
            }
        }
    }

    //最终赋值结果
    function setNavRow(obj) {
        $(obj).find("input[name='item_type']").val($("#pageType").children("option:selected").attr("value"));
        $(obj).find(".item_type").html($("#pageType").children("option:selected").html());
        $(obj).find("input[name='item_name']").val($("#urlKey").val());
        $(obj).find("input[name='item_path']").val($("#urlExp").val());
        //查找变量表达式
        var patternStr = $("#urlExp").val();
        var querystringStr = "";
        $("#var_box tr").each(function (i) {
            if ($(this).find("input[name='varName']").val() != "" && $(this).find("input[name='varExp']").val() != "") {
                patternStr = patternStr.replace("{" + i + "}", $(this).find("input[name='varExp']").val());
                querystringStr += $(this).find("input[name='varName']").val() + "=$" + (i + 1);
                if (i < $("#var_box tr").length - 1) {
                    querystringStr += "^";
                }
            }
        });
        $(obj).find("input[name='item_pattern']").val(patternStr);
        $(obj).find("input[name='item_querystring']").val(querystringStr);
        $(obj).find("input[name='item_templet']").val($("#pageTemplet").val() + ".html");
        $(obj).find("input[name='item_page']").val($("#urlPage").val() + ".aspx");
    }

    //检查Dialog输入情况
    function execChannelDialog(m, obj) {
        var oldkey = "";
        var objNum = arguments.length;
        if (objNum > 1) {
            oldkey = $(obj).parent().parent().find("input[name='item_name']").val();
        }
        if ($("#pageType").children("option:selected").attr("value") == "") {
            $.ligerDialog.warn('请选择频道类型!', function () {
                $("#pageType").focus();
            });
            return false;
        }
        if ($("#urlKey").val() == "") {
            $.ligerDialog.warn('请输入URL名称(Key)!', function () {
                $("#urlKey").focus();
            });
            return false;
        }
        if ($("#urlExp").val() == "") {
            $.ligerDialog.warn('请输入URL的重写表达式!', function () {
                $("#urlExp").focus();
            });
            return false;
        }
        if ($("#pageTemplet").val() == "") {
            $.ligerDialog.warn('请输入模板文件的名称!', function () {
                $("#pageTemplet").focus();
            });
            return false;
        }
        if ($("#urlPage").val() == "") {
            $.ligerDialog.warn('请输入生成ASPX文件的名称!', function () {
                $("#urlPage").focus();
            });
            return false;
        }
        //检查本地是否重复
        var checkKey = true;
        $("#item_box tr").each(function (i) {
            if ($("#urlKey").val() == $(this).find("input[name='item_name']").val() && $("#urlKey").val() != oldkey) {
                checkKey = false;
            }
        });
        if (!checkKey) {
            $.ligerDialog.warn('对不起,URL名称(Key)已重复啦!', function(){
                $("#urlKey").focus();
            });
            return false;
        }
        //AJAX验证
        var oldItemKey = "";
        if (objNum > 1) {
            if ($(obj).parent().parent().find("input[name='old_item_name']").length > 0) {
                oldItemKey = $(obj).parent().parent().find("input[name='old_item_name']").val();
            }
        }
        $.ajax({
            type: "post",
            url: "../../tools/admin_ajax.ashx?action=sys_urlrewrite_validate",
            data: {
                rewritekey: function () {
                    return $("#urlKey").val();
                },
                oldkey: function () {
                    return oldItemKey;
                }
            },
            dataType: "html",
            success: function (data, textStatus) {
                if (data == "true") {
                    if (objNum > 1) {
                        setNavRow($(obj).parent().parent());
                    } else {
                        //创建TR
                        var navSize = $('#item_box tr').size();
                        $("#item_box").append(getTr());
                        initButton(navSize);
                        setNavRow($("#item_box tr").eq(navSize));
                    }
                    m.close();
                } else {
                    $.ligerDialog.warn('对不起,AJAX检测URL名称(Key)已存在,若旧名称已更改,请保存后方可使用该名称!', function () {
                        $("#urlKey").focus();
                    });
                    return false;
                }
            }
        });
    }
</script>
</head>
<body class="mainbody">
<form id="form1" runat="server">
<div class="navigation"><a href="javascript:history.go(-1);" class="back">后退</a>首页 &gt; 控制面板 &gt; 系统频道</div>
<div id="navtips" class="navtips">
    编辑频道信息需具备基本的正则表达式知识,注意URL配置的名称不要重复,以下正则表达式可供参考:<br />
    约定参数:category_id为频道ID,page为分页页码;<br />
    <a href="javascript:CloseTip('navtips');" class="close">关闭</a>
</div>
<div id="contentTab">
    <ul class="tab_nav">
        <li class="selected"><a οnclick="tabs('#contentTab',0);" href="javascript:;">编辑频道信息</a></li>
    </ul>
    <div class="tab_con" style="display:block;">
        <table class="form_table">
            <col width="180px"><col>
            <tbody>
            <tr>
                <th>频道名称:</th>
                <td>
                    <asp:HiddenField ID="hidName" runat="server" Value="" />
                    <asp:TextBox ID="txtName" runat="server" CssClass="txtInput normal ime-disabled"></asp:TextBox><label>*只允许英文字母、下划线,不可重复</label></td>
            </tr>
            <tr>
                <th>频道标题:</th>
                <td><asp:TextBox ID="txtTitle" runat="server" CssClass="txtInput normal required" minlength="2" maxlength="100"></asp:TextBox><label>*</label></td>
            </tr>
            <tr>
                <th>所属模型:</th>
                <td><asp:DropDownList id="ddlModelId" CssClass="select2 required" runat="server"></asp:DropDownList><label>*更改模型请手动删除旧内容</label></td>
            </tr>
            <tr>
                <th>排 序:</th>
                <td><asp:TextBox ID="txtSortId" runat="server" CssClass="txtInput normal small required digits"></asp:TextBox><label>*</label></td>
            </tr>
            <tr>
                <th>URL重写配置:</th>
                <td><button id="itemAddButton" type="button" class="btnSelect"><span class="add">添 加</span></button></td>
            </tr>
            <tr>
                <th></th>
                <td>
                    <table border="0" cellspacing="0" cellpadding="0" class="border_table" width="99%">
                        <thead>
                        <tr>
                            <th width="9%">类型</th>
                            <th width="15%">名称</th>
                            <th width="18%">URL重写</th>
                            <th width="18%">正则表达式</th>
                            <th width="18%">参数配置</th>
                            <th width="15%">模板文件</th>
                            <th width="5%">操作</th>
                        </tr>
                        </thead>
                        <tbody id="item_box">
                        <asp:Repeater ID="rptList" runat="server">
                        <ItemTemplate>
                        <tr class="td_c">
                            <td>
                                <input type="hidden" name="item_type" value="<%#Eval("type")%>" />
                                <input type="hidden" name="item_page" value="<%#Eval("page")%>" />
                                <span class="item_type"><%#GetPageTypeName(Eval("type").ToString())%></span>
                            </td>
                            <td><input type="hidden" name="old_item_name" value="<%#Eval("name")%>" /><input name="item_name" type="text" value="<%#Eval("name")%>" class="txtInput small" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_path" type="text" value="<%#Eval("path")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_pattern" type="text" value="<%#Eval("pattern")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_querystring" type="text" value="<%#Eval("querystring")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_templet" type="text" value="<%#Eval("templet")%>" class="txtInput small" style="width:98%;" readonly="readonly" /></td>
                            <td><img alt="编辑" src="../images/icon_edit.gif" class="operator" /><img alt="删除" src="../images/icon_del.gif" class="operator" /></td>
                        </tr>
                        </ItemTemplate>
                        </asp:Repeater>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <th></th>
                <td><asp:Button ID="btnSubmit" runat="server" Text="提交保存" CssClass="btnSubmit" οnclick="btnSubmit_Click" /></td>
            </tr>
            </tbody>
        </table>
    </div>
    
</div>
</form>
</body>
</html>

 

 


 

 

 

转载于:https://www.cnblogs.com/kennyliu/p/3379032.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值