代码之家  ›  专栏  ›  技术社区  ›  Maverick

在ASP.NET页上使用服务器端标记时主题不起作用

  •  1
  • Maverick  · 技术社区  · 16 年前

    ASP.NET页的代码为:

    <div class="facebox_content">
    <% if (CurrentUser.Role == "Free")
       {
    %>
    <table cellpadding="0" cellspacing="0" style="border-collapse:collapse;width:380px;">
        <tr>
            <td>
                User Name :
            </td>
            <td>
                Membership Cost :
            </td>
        </tr>
        <tr>
            <td style="width:190px;">
                <asp:TextBox ID="txtUserName" Enabled="false" runat="server" Text="<%= CurrentUser.Name %>"/>
            </td>
            <td style="width:190px;">
                <asp:TextBox ID="txtCost" Enabled="false" runat="server" Text="2000"/>
    
            </td>
        </tr>
        <tr>
            <td>
                <br />
                Cheque / Draft No.:
            </td>
            <td>
                <br />
                Bank Drawn On :
            </td>
        </tr>
        <tr>
            <td style="width:190px;">
                <asp:TextBox ID="txtChqNo" runat="server"></asp:TextBox>
            </td>
            <td style="width:190px;">
                <asp:TextBox ID="txtBankName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <br />
                Date :
            </td>
            <td>
                <br />
                City :
            </td>
        </tr>
        <tr>
            <td style="width:190px;">
                <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
            </td>
            <td style="width:190px;">
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
    <% 
       }
       else if(CurrentUser.Role == "Pending")
       {
    %>
    <p style="text-align:justify;">
        Your Request is pending with our Administrators.
        Please be patient while your request is processed.
        Usually it takes 2-4 Days for your request to be processed after the payment has been received. 
    </p>
    <% 
       }
       else if(CurrentUser.Role == "Paid")
       {
    %>
    <p style="text-align:justify;">
        You are already a Paid Member of Website
    </p>
    <% 
       }
    %>
    

    C文件的代码是:

    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Theme = CurrentUser.Theme;
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUserName.Text = CurrentUser.Name;
        ConfirmButton.Attributes.Add("onclick", "javascript:document.getElementById('" + lblMsg.ClientID + "').style.display='none';");
    
        if (CurrentUser.Role != "Free")
            ConfirmButton.Visible = false;
    }
    

    代码出现以下错误:

    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace: 
    
    [HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
       System.Web.UI.ControlCollection.Add(Control child) +8678903
       System.Web.UI.PageTheme.SetStyleSheet() +478
       System.Web.UI.Page.OnInit(EventArgs e) +8699660
       System.Web.UI.Control.InitRecursive(Control namingContainer) +333
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378
    

    请有人帮我…!

    1 回复  |  直到 16 年前
        1
  •  3
  •   Mouhong Lin    16 年前

    也许你可以用另一种方式:

    Aspx:

    <asp:PlaceHolder runat="server" ID="FreeHolder" Visible="false">
        <table> ... </table>
    </asp:PlaceHolder>
    <asp:PlaceHolder runat="server" ID="PendingHolder" Visible="false">
      <p style="text-align:justify;">
        Your Request is pending with our Administrators. ...
      </p>
    </asp:PlaceHolder/>
    <asp:PlaceHolder runat="server" ID="PaidHolder" Visible="false">
    ...
    </asp:PlaceHolder/>
    

    代码后面:

    if (CurrentUser.Role == "Free")
    {
        FreeHolder.Visible = true;
    } else if (CurrentUser.Role == "Pending") {
        PendingHolder.Visible = true;
    } else if (CurrentUser.Role == "Paid") {
        PaidHolder.Visible = true;
    }