0

I'm trying to do the following in a aspx page:

<%@ Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
    <% if (!isOld) %>
    <% { %>
        <p>display this</p>
    <% } %>

</asp:Content>

isOld is a public bool variable from the cs file mentioned in the namespace.

But unfortunately it gave me an unknown error.

I could do something similar in JSPs, but after googling around for a while, I'm not so sure whether the above is achievable in ASP.NET? (Am I missing a tag declaration, or do I have to write the entire tag lib myself?)

Thanks.

EDIT: I just got an unknown error. I have a feeling that the code above has either the wrong syntax or are totally off the wrong track. I tried the following code, and there was no error, but the bool variable is always false:

<% #if !isOld %>
    <p> display this</p>
<% #endif %>
2
  • And what error was it? Something like: "Cannot modify the controls collection"? If so, then you're just out of luck. You need to stop the code that is modiying the controls (by adding or removing one, somewhere in your codebehind) or make the 'display this' appear in a PlaceHolder and set it's Visible property appropriately (this is what I would do). Commented Mar 2, 2010 at 7:59
  • Re: your edit, you're missing a bracket: <% if (!isOld) %> should be <% if (!isOld) { %> (note bracket). (Edit: actually, perhaps not, I've never seen anyone write the bracket in a totally different <% %> area though, it seems odd .. Commented Mar 2, 2010 at 8:10

1 Answer 1

3

in your code front:

<%@ Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
    <asp:PlaceHolder runat="server" id="PlaceHolderIsOld">
        <p>display this</p>
    </asp:PlaceHolder>
</asp:Content>

and then in your code behind:

protected void Page_Load(object sender, EventArgs e){
   PlaceHolderIsOld.Visible = IsOld;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@AndreasKnudsen: where does PlaceHolderIsOld live in the code behind? My code behind inherits System.Web.UI.Page and I do not have a Page_Load method. I can't seem to access PlaceHolderIsOld in anyway.
By defining the placeholder in your codefront it will automatically appear as useable in your codebehind. (Visual studio generates the code in the *.designer.cs file) Asp.Net is hard-wired to look for certain methods in pages and user controls using reflection. if you define a method with the signature as presented in the code above then Asp.Net will call it at the "Load" part of the page lifecycle. though mildly cryptic especially for advanced sites using nested controls and masterpages it's well worth learning: coolwebdeveloper.com/downloads/ASPNET20PageLifeCycle.jpg
What I ended up doing was something similar to your code above: setting everything on the server side. I made the item/content area visible when certain conditions were met etc instead of using asp syntax. Worked just as well. Thanks for the idea and also the diagram too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.