Search code examples
c#asp.netext.net

Use C# to write ASP code on a page


I have ASP code like this:

<ext:Panel ID="pnlHelp" CtCls="help-panel" AnimCollapse="true"">
    <Content>
        <h1>some text</h1>
        <p>
            More text[...]
        </p>
    </Content>
</ext:Panel>

I would like to generate the <Content> tag dynamically using C#. I tried this, like with regular HTML tags:

<ext:Panel ID="pnlHelp" CtCls="help-panel" AnimCollapse="true"">
    <Content>
        <% Response.Write("<h1>some text</h1>"); %>                             
        <p>
            More text[...]
        </p>
    </Content>
</ext:Panel>

But the text ends up somewhere near the beginning of the page where I didn't intend it to go. How can I do this?


Solution

  • You output appears on the top of the page because your Response.Write() is being executed before page content is put to respose.

    Why not just

    <%="<h1>some text</h1>" %> 
    

    You can create a method that will return a string and call it from your *.as?x file:

    protected string GetMyCoolHtml()
    {
        return "<h3>this is my text</h3>";
    }
    
    ....
    
    <%= GetMyCoolHtml() %>