Creating a Web.config Editor - Part 1
page 5 of 5
by Jason N. Gaylord
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25618/ 41

Section 3: Implementing The Page
Now that our document is set, we must then add code to edit this node. In this case, we will use a drop down box with the correct values of this node. We will then edit the web.config file by setting the value equal to the returned value of the drop-down control. Finally, we need to save the web.config file to store our new settings. An example of this is shown below:

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
9:          myConfig.Load("C:\InetPub\wwwroot\web.config")
11:           If not isPostBack then
12:              Dim myCurrentValue as String
13:              Dim myListItem as ListItem
15:              myCurrentValue = myConfig.SelectSingleNode(
16:              myListItem = node01.Items.FindByText(myCurrentValue)
18:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
19:           Else
20:             'We will save this for later!
21:           End If
23:        End Sub
25:        Sub Button_Click(s as Object, e as EventArgs)
26:           Dim myConfig As New XmlDocument()
27:           Dim myAttribute As XmlAttribute
29:           myConfig.Load("C:\InetPub\wwwroot\web.config")
31:           myAttribute = myConfig.SelectSingleNode(
32:           myAttribute.Value = node01.SelectedItem.ToString()
34:           myConfig.Save("C:\InetPub\wwwroot\web.config")
35:        End Sub
37:    </script>
38:    <html>
39:    <head>
40:        <title>Web.config Editor</title>
41:    </head>
42:    <body>
43:        <form runat="server">
44:          <asp:Label id="title01" runat="server" text="Custom Errors:" />
45:          <asp:DropDownList id="node01" runat="server">
46:            <asp:ListItem Value="On" text="On" />
47:            <asp:ListItem Value="Off" text="Off" />
48:            <asp:ListItem Value="RemoteOnly" text="RemoteOnly" />
49:          </asp:DropDownList>
50:          <br />
1:          <asp:Button id="myButton" runat="server" Text="Update"
                 OnClick="Button_Click" />
52:        </form>
53:    </body>
54:    </html>

Now that we have changed a pre-defined node, let's change a custom node. This node will have a similar node, but with a different property. This will be demonstrated in Part 2. You can go to Part 2 by clicking here.

View Entire Article

User Comments

Title: I Have question   
Date: 2006-05-08 2:25:38 AM
How to use Location Tag in web.config file?
and .......... Thanks !!!
Title: Excellent trial for NewToXml   
Name: Baljeet Kiroriwal
Date: 2006-03-02 7:46:07 AM
Good Article n gr8 information
Title: What if the solution is in a source safe   
Name: m7e7
Date: 2004-08-05 4:03:37 PM
What if the soln is in a source safe vault?
Title: The problems I had   
Name: Sergio Florez
Date: 2004-07-23 9:51:21 AM
First, sorry if I mix up the first and second part of the article but here goes:

You're capturing the return of SelectSingleNode() in an XmlAttribute and that didn't work for me. It said it needed a NodeSet. I fixed this by capturing it with an XmlNode and working with its Attributes collection.

The other problem I had was that your XPath didn't capture the node for me so I had to use this:
Title: RE: Thanks but no thanks   
Name: Jason
Date: 2004-07-22 4:35:10 PM
I wish you'd let me know what isn't working. This way I can provide some "troubleshooting" for you.
Title: Thanks but no thanks   
Name: Sergio Florez
Date: 2004-07-22 11:31:08 AM
This article's basic concept of trying to modify the web.config is great, unfortunately the code doesn't work for me. Maybe you should try and write some troubleshooting for this.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-23 1:23:16 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search