private void ButtonSave_Click(object sender, System.EventArgs e)
{
Modify("Style",DDListSkin.SelectedValue.ToString());
Modify("Addr",TxtCompanyAddr.Text.ToString());
Modify("Tel",TxtCompanyTel.Text.ToString());
Modify("Email",TxtCompanyEmail.Text.ToString());
Modify("User",TxtAdmin.Text.ToString());
Modify("Pass",TxtPassword.Text.ToString());
Modify("SiteName",TxtSiteNameCN.Text.ToString());
Modify("EnglishName",TxtSiteNameEN.Text.ToString());
}
private void Modify(string key,string strValue)
{
string XPath="/configuration/appSettings/add[@key='?']";
XmlDocument domWebConfig=new XmlDocument();
domWebConfig.Load( (HttpContext.Current.Server.MapPath("web.config")) );
XmlNode addKey=domWebConfig.SelectSingleNode( (XPath.Replace("?",key)) );
if(addKey == null)
{
throw new ArgumentException("没有找到<add key="+key+" value=.../>的配置节");
}
addKey.Attributes["value"].InnerText=strValue;
domWebConfig.Save( (HttpContext.Current.Server.MapPath("web.config")) );
}
========================================================================================
以上代码只有dropdownlist的值可以修改web.config里的appSettings
文本框里的值确不能修改,是不是我的文本框取值错误?百思不得其解!!高人指明!!
{
Modify("Style",DDListSkin.SelectedValue.ToString());
Modify("Addr",TxtCompanyAddr.Text.ToString());
Modify("Tel",TxtCompanyTel.Text.ToString());
Modify("Email",TxtCompanyEmail.Text.ToString());
Modify("User",TxtAdmin.Text.ToString());
Modify("Pass",TxtPassword.Text.ToString());
Modify("SiteName",TxtSiteNameCN.Text.ToString());
Modify("EnglishName",TxtSiteNameEN.Text.ToString());
}
private void Modify(string key,string strValue)
{
string XPath="/configuration/appSettings/add[@key='?']";
XmlDocument domWebConfig=new XmlDocument();
domWebConfig.Load( (HttpContext.Current.Server.MapPath("web.config")) );
XmlNode addKey=domWebConfig.SelectSingleNode( (XPath.Replace("?",key)) );
if(addKey == null)
{
throw new ArgumentException("没有找到<add key="+key+" value=.../>的配置节");
}
addKey.Attributes["value"].InnerText=strValue;
domWebConfig.Save( (HttpContext.Current.Server.MapPath("web.config")) );
}
========================================================================================
以上代码只有dropdownlist的值可以修改web.config里的appSettings
文本框里的值确不能修改,是不是我的文本框取值错误?百思不得其解!!高人指明!!
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path); //如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
} //判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
// xn1.ParentNode.RemoveChild(xn1);
break;
}
} //当前节点不存在,则添加新节点
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add"); //添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa); //添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
return true;
}
catch
{
return false;
}
}
把代码贴出来了web.config
<appSettings>
<add key="ConnectionString" value="db/data.mdb" />
<add key="Style" value="red" />
<add key="Addr" value="LaoDai.Net" />
<add key="Tel" value="vTel" />
<add key="Email" value="EmailEmail" />
<add key="User" value="User" />
</appSettings>aspx<%@ Page language="c#" Codebehind="Sample_ModifyWebConfig.aspx.cs" AutoEventWireup="false" Inherits="Test1.Sample_ModifyWebConfig" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Sample_ModifyWebConfig</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DropDownList id="dropSkin" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 112px"
runat="server">
<asp:ListItem Value="blue">blue</asp:ListItem>
<asp:ListItem Value="red">red</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtCompany" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 112px"
runat="server"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 216px; POSITION: absolute; TOP: 288px" runat="server"
Text="Button"></asp:Button></FONT>
</form>
</body>
</HTML>
aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;namespace Test1
{
/// <summary>
/// Sample_ModifyWebConfig 的摘要说明。
/// </summary>
public class Sample_ModifyWebConfig : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList dropSkin;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox txtCompany;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string webConfigDirectory = Server.MapPath(".");
//Response.Write(webConfigDirectory);
AppSettingsEdit(webConfigDirectory, "Style",dropSkin.SelectedItem.Value);
AppSettingsEdit(webConfigDirectory, "Addr",txtCompany.Text);
} private void AppSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path); //如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
} //判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
// xn1.ParentNode.RemoveChild(xn1);
break;
}
} //当前节点不存在,则添加新节点
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add"); //添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa); //添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
//return true;
}
catch
{
//return false;
throw new Exception("error");
}
} }
}
~~~~~~~~忧闷中......
没有大虾帮忙吗???加到100分啦!!!!
我看到了一个网站 100MB asp和 asp.net空间 现在促销,
同学们可以做一个简单的个人网站用来找工作 ,
支持 asp.net 一年才50元!不要错过哦!!!
速度非常快,一般公司企业 足够用了!
http://www.hi876.com
希望能对大家有帮助
没人能解释一下吗,为什么dropdownlist的值可以改web.config的值,而其它的不能改??
这样应该不是权限的问题了,那是那里的问题呢???困扰我三天了!!!
{Modify("Style",DDListSkin.SelectedValue.ToString());
Modify("Addr",TxtCompanyAddr.Text.ToString()); //确认这里取到了Text的值?
Modify("Tel",TxtCompanyTel.Text.ToString());
}一般ASP.NET中容易出现问题的是状态或者值的保持问题。
Modify("Style",DDListSkin.SelectedValue.ToString());
Modify("Addr",TxtCompanyAddr.Text.ToString());在Modify了第一项值后,由于修改了web.config文件,系统需要重新进入,下面的程序是否还能继续执行呢?
值是肯定取到,如果说修改第一项值后,系统会重启引起后面不执行的话!
我改变了执行顺序,也只有style这项会修改,其它一样不动!真是奇怪!!