Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8573825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:20:20+00:00 2026-06-11T19:20:20+00:00

I am building a database editor/line-of-business tool for a client using .NET 4.0, and

  • 0

I am building a database editor/line-of-business tool for a client using .NET 4.0, and IIS 7. I want to conditionally include some HTML in my page based on values I have stored in Session. I am doing this on the server-side, and to encapsulate the code, I created an ASP Server Control.

As you will see, the markup generated by the Server Control is not what I expected. I am hoping someone out there has seen this before and can help me understand how to control the markup generation output.

Here’s the new RenderContents for a control called MyList. It is supposed to generate new list entries using the <li> tag.

protected override void RenderContents(HtmlTextWriter output) {
    output.RenderBeginTag(HtmlTextWriterTag.Li);
    output.WriteEncodedText(this.Text);
    output.RenderEndTag();
}

After compiling the main project, and adding a reference to MyList, I use MyList in the following HTML:

<h1>Favorite Things</h1>
<ul>
    <cc1:MyList ID="mL1" runat="server" Text="Code that works!" />
    <cc1:MyList ID="mL2" runat="server" Text="Going home before 8" />
    <cc1:MyList ID="mL3" runat="server" Text="Cold drinks in fridge" />
</ul>

And it generates the following:

<h1>Favorite Things</h1>
<ul>
    <span id="MainContent_mL1"><li>Code that works!</li></span>
    <span id="MainContent_mL2"><li>Going home before 8</li></span>
    <span id="MainContent_mL3"><li>Cold drinks in fridge</li></span>
</ul>

Now I add a test based on a Session value. The Page property of WebControl provides me a reference to the control’s container, and thus access to my Session:

protected override void RenderContents(HtmlTextWriter output) {
    string backcolor = "Yellow";
    if (this.Page.Session["access"] == null) {
        backcolor = "Red";
    }
    output.RenderBeginTag(HtmlTextWriterTag.Li);
    output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, backcolor);
    output.WriteEncodedText(this.Text);
    output.RenderEndTag();
}

Now the markup begins to unravel. Note the inconsistency in ‘mL1’:

<h1>Favorite Things</h1>
<ul>
    <span id="MainContent_mL1"><li>Code that works!</li></span>
    <span id="MainContent_mL2" style="background-color:Red;"><li>Going home before 8</li></span>
    <span id="MainContent_mL3" style="background-color:Red;"><li>Cold drinks in fridge</li></span>
</ul>

In my real code, which is more complex, the markup turned into just the span tags. And, when I set breakpoints in RenderContents() it only got called once when I had five tags in a row.

Other info:
The page with cc1:MyList controls has EnableSession=true. My web.config specifies the normal Session manager (‘rml’, and ‘RoleBasedList’ refer to my ‘real’ control, which I simplified to isolate the problem and make this post shorter):

<system.web>
    <trace enabled="true" localOnly="false" pageOutput="true" requestLimit="20" />
    <compilation debug="true" targetFramework="4.0" />
    <pages>
        <controls>
            <add tagPrefix="rml" assembly="RoleBasedList" namespace="SOTS.ServerControls"/>
        </controls>
    </pages>
    <httpModules>
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    </httpModules>

    <sessionState mode="InProc" cookieless="false" timeout="60"/>
    ...
</system.web>

And now you know everything I do!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:20:21+00:00Added an answer on June 11, 2026 at 7:20 pm

    You just need to override the WebControl.TagKey property in your custom server control:

    protected override HtmlTextWriterTag TagKey
    {
        get { return HtmlTextWriterTag.Li; }
    }
    

    The default value is Span, which explains what you’re seeing. Of course, if you do this, you don’t render the Li tag in your RenderContents override.

    An alternative would be to override Render, to take full control of the rendering, or to derive from Control instead. But you’ll lose some of the features of WebControl, notably styles for your outer tag.

    Regarding your second problem, you are calling:

    output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, backcolor);
    

    which adds a style attribute to be applied to the next call to RenderBeginTag. You don’t call RenderBeginTag, so this is applied to a tag in the next control in the tree.

    Another point is Session will be null when your control is rendered in the Visual Studio designer. You should check for null, or alternatively check if you’re running in design mode:

    if ((Site != null) && (Site.DesignMode))
    {
        ... running in design mode, Session won't be available
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a database + tool that scours news feeds for a certain term.
I'm building a database library for my application using sqlite3 as the base. I
I'm using a sqlite3 database to store app's data. Instead of building a database
i am building a database application in vb.net and i started by adding a
For the purpose of building a database system I am using a simple builder
I have a little problem, I am building a database from CSV files using
I have another Microsoft Access question: I am building a contact database, and some
I am building a database using SQL Server 2008 to store prices of securities
So I have some experience with Microsoft Access, building database apps for people, vba,
I'm building an admin panel for some website, and I want to add the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.