AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=83&pId=-1
Creating a Web.config Editor - Part 2
page
by Jason N. Gaylord
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21963/ 37

Before We Begin
Note: This is a continuation of a previous article. You can view the first part by clicking here
Section 1: Setting Up The Web.Config File
Now that we know how to edit basic properties in the web.config, let's create some custom properties. For this example, I have added two custom nodes as shown below:
EditConfig.aspx
**************************************************

<configuration>
  <appSettings>
    <add key="conUser" Value="sa">
    <add key="conServer" value="localhost">
  </appSettings>
</configuration>
Section 2: Prepare Our Page
We will target the server node for this example. We will setup our page to include a few servers in a drop-down like the previous section demonstrated. We will add values of 'Server01', 'Server02', and 'Server03'. After adding these, our code should look like this:
EditConfig.aspx
**************************************************

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
5:   
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
8:       
9:          myConfig.Load("C:\InetPub\wwwroot\web.config")
10:        
11:           If not isPostBack then
12:              Dim myCurrentValue as String
13:              Dim myListItem as ListItem
14:        
15:              myCurrentValue = myConfig.SelectSingleNode(
                  "configuration/appSettings/add[@key='conServer']
                  /@value").Value
16:              myListItem = node01.Items.FindByText(myCurrentValue)
17:        
18:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
19:           Else
20:             'We will save this for later!
21:           End If
22:        
23:        End Sub
24:    
25:    </script>
26:    <html>
27:    <head>
28:        <title>Web.config Editor</title>
29:    </head>
30:    <body>
31:        <form runat="server">
32:          <asp:Label id="title01" runat="server" text="Change Server:" />
33:          <asp:DropDownList id="node01" runat="server">
34:            <asp:ListItem Value="localhost" text="localhost" />
35:            <asp:ListItem Value="Server01" text="Server01" />
36:            <asp:ListItem Value="Server02" text="Server02" />
37:            <asp:ListItem Value="Server03" text="Server03" />
38:          </asp:DropDownList>
39:          <br />
40:        </form>
41:    </body>
42:    </html>

Notice how the SelectSingleNode option changed. We are now using a parameter to select which 'add' node we want to use. Think of this as a SELECT SQL statement. We are selecting the node where 'key' is equal to 'conServer'. The attribute of the node is assigned by using the ampersand(@) symbol. Then, the value you want to select is placed inside single quotes('). Finally, the entire statement is placed in between brackets([ and ]).
Section 3: Implementing The Page
Then, we will use the same concept as Part 1 to save the node. This time we will use the a parameter to select which 'add' node we want to update.
EditConfig.aspx
**************************************************

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
5:   
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
8:       
9:          myConfig.Load("C:\InetPub\wwwroot\web.config")
10:        
11:           If not isPostBack then
12:              Dim myCurrentValue as String
13:              Dim myListItem as ListItem
14:        
15:              myCurrentValue = myConfig.SelectSingleNode(
                  "configuration/appSettings/add[@key='conServer']
                  /@value").Value
16:              myListItem = node01.Items.FindByText(myCurrentValue)
17:        
18:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
19:           Else
20:             'We will save this for later!
21:           End If
22:        
23:        End Sub
24:        
25:        Sub Button_Click(s as Object, e as EventArgs)
26:           Dim myConfig As New XmlDocument()
27:           Dim myAttribute As XmlAttribute
28:        
29:           myConfig.Load("C:\InetPub\wwwroot\web.config")
30:           
31:           myAttribute = myConfig.SelectSingleNode(
                 "configuration/appSettings/add[@key='conServer']
                 /@value")
32:           myAttribute.Value = node01.SelectedItem.ToString()
33:           
34:           myConfig.Save("C:\InetPub\wwwroot\web.config")
35:        End Sub
36:    
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="Change Server:" />
45:          <asp:DropDownList id="node01" runat="server">
46:            <asp:ListItem Value="localhost" text="localhost" />
47:            <asp:ListItem Value="Server01" text="Server01" />
48:            <asp:ListItem Value="Server02" text="Server02" />
49:            <asp:ListItem Value="Server03" text="Server03" />
50:          </asp:DropDownList>
51:          <br />
52:          <asp:Button id="myButton" runat="server" Text="Update"
                 OnClick="Button_Click" />
53:        </form>
54:    </body>
55:    </html>

Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 5:32:55 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search