关闭

asp.net修改web.config

872人阅读 评论(0) 收藏 举报

前台代码

 

<%@ Page Language="C#" AutoEventWireup="true" Debug="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<table>
    
<tr><td>key</td><td><asp:DropDownList ID="ddlst" runat="server" AutoPostBack="true" /></td></tr>
    
<tr><td></td><td><asp:TextBox ID="txtPre" runat="server" /></td></tr>
    
<tr><td></td><td><asp:TextBox ID="txtEnd" runat="server" /></td></tr>
    
<tr><td colspan="2"><asp:Button ID="btn" runat="server" Text="修改" OnClick="btn_Click" /></td></tr>
    
</table>
    
</div>
    
</form>
</body>
</html>

 

后台代码:

 

 

using System;
using System.Data;
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 System.Web.Configuration;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
this.ddlst.SelectedIndexChanged += new EventHandler(ddlst_SelectedIndexChanged);
        
if (!IsPostBack)
        {
            BindList();           
            
this.txtPre.Text = this.ddlst.SelectedValue;
        }
    }

    
private void ddlst_SelectedIndexChanged(object sender, EventArgs e)
    {
       
this.txtPre.Text = this.ddlst.SelectedValue;
    }

    
private void BindList()
    {
        
string strFileName = Server.MapPath("."+ @"Web.config";
        XmlDocument doc 
= new XmlDocument();
        doc.Load(strFileName);
        XmlNodeList lstTop 
= doc.DocumentElement.ChildNodes;
        
foreach (XmlElement el in lstTop)
        {
            
//Response.Write(el.Name + ":top node name:<br />");
            if (el.Name.ToLower() == "appsettings")
            {
                XmlNodeList lstApp 
= el.ChildNodes;
                
if (lstApp.Count > 0)
                {
                    
this.ddlst.Items.Clear();
                    
foreach (XmlElement elApp in lstApp)
                    {                      
                       
this.ddlst.Items.Add(new ListItem(elApp.Attributes["key"].InnerXml,elApp.Attributes["value"].Value));
                    }
                }
                
break;                
            }
        }
    }

    
public void btn_Click(object sender, EventArgs e)
    {
        
//Response.Write("selected value:" + this.ddlst.SelectedValue + "<br />");
        string strFileName = Server.MapPath("."+ @"Web.config";
        XmlDocument doc 
= new XmlDocument();
        doc.Load(strFileName);
        XmlNodeList lstTop 
= doc.DocumentElement.ChildNodes;
        
foreach (XmlElement el in lstTop)
        {        
            
if (el.Name.ToLower() == "appsettings")
            {
                XmlNodeList lstApp 
= el.ChildNodes;
                
if (lstApp.Count > 0)
                {                
                    
foreach (XmlElement elApp in lstApp)
                    {
                       
// Response.Write("key name:" + elApp.Attributes["key"].InnerXml + "<br />");
                       
// Response.Write("selectedvalue:" + this.ddlst.SelectedValue.ToLower() + "<br />");
                        if (this.ddlst.SelectedItem.Text.ToLower() == elApp.Attributes["key"].InnerXml.ToLower())
                        {
                            
//Response.Write("key name:" + elApp.Attributes["key"].InnerXml + "<br />");
                           
// Response.Write("value:" + this.txtEnd.Text + "<br />");
                            elApp.Attributes["value"].Value = this.txtEnd.Text;
                        }
                    }
                }
            }
        }
        doc.Save(strFileName);
        BindList();
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:391172次
    • 积分:6268
    • 等级:
    • 排名:第3914名
    • 原创:196篇
    • 转载:121篇
    • 译文:1篇
    • 评论:55条
    文章分类
    最新评论