Modify the configuration items in Web. config dynamically in ASP. NET)

Source: Internet
Author: User
Modify the configuration items in Web. config dynamically in ASP. NET)

Front-end Page code
<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "webapplication1.webform1" %> 〉
<〈! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> "〉
<HTML> 〉
<Head> 〉
<Meta name = "generator" content = "Microsoft Visual Studio 7.0"> "〉
<Meta name = "code_language" content = "C #"> #"〉
<Meta name = "vs_defaultclientscript" content = "javascript (ecmascript)"> )"〉
<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: textbox id = "textbox1" style = "Z-INDEX: 101; left: 164px; position: absolute; top: export PX "runat =" server "width =" 204px "Height =" 25px "> </ASP: textbox> 〉
<Asp: button id = "button1" style = "Z-INDEX: 102; left: pixel; position: absolute; top: 188px "runat =" server "width =" 78px "Height =" 25px "text =" button "> </ASP: button> 〉
<Asp: dropdownlist id = "dropdownlist1" style = "Z-INDEX: 103; left: 29px; position: absolute; top: 192px "runat =" server "width =" 130px "Height =" 22px "> </ASP: dropdownlist> 〉
</Font> 〉
</Form> 〉
</Body> 〉
</Html> 〉

Backend CS code

Using 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 webapplication1
{
/// <Summary> 〉
/// Summary description for webform1.
/// </Summary> 〉
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox textbox1 ;;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1 ;;
Protected system. Web. UI. webcontrols. Button button1 ;;

Public webform1 ()
{
Page. init + = new system. eventhandler (page_init );;
}

Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
// Open a file (for example, Web. Config in the root directory)
String filename = server. mappath ("/") + @ "\ WEB. config ";;
Xmldocument xmldoc = new xmldocument ();;
Xmldoc. Load (filename );;

Xmlnodelist topm = xmldoc. documentelement. childnodes ;;
Foreach (xmlelement element in topm)
{
If (element. Name. tolower () = "appsettings ")
{
Xmlnodelist _ node = element. childnodes ;;
If (_ node. Count> 0)
{
Dropdownlist1.items. Clear ();;
Foreach (xmlelement El in _ node)
{
Dropdownlist1.items. Add (El. attributes ["key"]. innerxml );;
}
}
}
}
}
}

Private void page_init (Object sender, eventargs E)
{
Initializecomponent ();;
}

# Region web form designer generated code
/// <Summary> 〉
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary> 〉
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );;
This. Load + = new system. eventhandler (this. page_load );;

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
String filename = server. mappath ("/") + @ "\ WEB. config ";;
Xmldocument xmldoc = new xmldocument ();;
Xmldoc. Load (filename );;

Xmlnodelist topm = xmldoc. documentelement. childnodes ;;
Foreach (xmlelement element in topm)
{
If (element. Name. tolower () = "appsettings ")
{
Xmlnodelist _ node = element. childnodes ;;
If (_ node. Count> 0)
{
Foreach (xmlelement El in _ node)
{
If (El. attributes ["key"]. innerxml. tolower () = This. dropdownlist1.selecteditem. value. tolower ())
{
El. attributes ["value"]. value = This. textbox1.text ;;
}
}
}
}
}
Xmldoc. Save (filename );;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.