asp.net修改web.config

前台代码

 

<%@ 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();
    }
}
阅读更多
个人分类:
想对作者说点什么? 我来说一句

asp.netweb.config设置默认首页

cdefg198 cdefg198

2012-10-16 17:37:51

阅读数:10576

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭