开发者

Need help using ConfigurationManager from aspx page

New to ASP.NET. Trying to be able to switch what text is being displayed based off a value in the Web.config file. Here is a sample of my code.

<td background="images/LoginBox_03.gif" width="350" height="151">
                <table border="0" align="center" id="tblLogin" runat="server">
                    <tr id="trEmail" runat="server">
                        <%
                        If System.Configuration.ConfigurationManager.AppSettings("AD") <> "True" Then
                        %>
                        <td>
                            Email:
                        </td>
                        <%
                        ElseIf System.Configuration.ConfigurationManager.AppSettings("AD") = "False" Then
                        %>
                        <td>
                            Username:
                        </td>
                        <%
                        End If
                        %>
                        <td>
                            <asp:TextBox ID="txtEmail" runat="server" Width="145px" />
                        </td>
                        <td>

Visual Studio doesnt like this for some reason. The code is not highlighted like it is actually code. More like it is just text. I do not开发者_Python百科 think the VB.NET code is inserted correctly. Could someone help point out what is wrong here?


Instead of using inline code as you have above, IMO, the better way is to use a<asp:Label .../> control.

The return values from System.Configuration.ConfigurationManager.AppSettings("AD") is of type String you must first cast it to a Boolean then check the value.


Have you considered moving the logic to the code behind file? It will clean up your page a little.

aspx:

<td background="images/LoginBox_03.gif" width="350" height="151">
                <table border="0" align="center" id="tblLogin" runat="server">
                    <tr id="trEmail" runat="server">
                        <td>
                            <asp:label id="lblFoo" runat="server" />
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmail" runat="server" Width="145px" />
                        </td>
                        <td>
                    </tr>
                 </table>

Code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If System.Configuration.ConfigurationManager.AppSettings("AD") <> "True" Then
         lblFoo.text = "UserName"
Else
         lblFoo.text = "Email"
End If


    End Sub


I'm not sure you can write fragments of code like this...

<tr>
    <%
    If X Then
        Response.Write "<td>blabla</td>"
    Else
        Response.Write "<td>omglol</td>"
    End If
    %>
</tr>

would work better I think.

I also noticed somthing strange in your If...ElseIf statement.

If System.Configuration.ConfigurationManager.AppSettings("AD") <> "True" Then
    'Happens if False.
ElseIf System.Configuration.ConfigurationManager.AppSettings("AD") = "False" Then
    'Also happens if false
EndIf

Because if it is different than true, then it is false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜