how to write valid xhtml when using server controls

Rate It (1)

Last post 01-08-2009 7:53 AM by TATWORTH. 2 replies.

Sort Posts:

  • how to write valid xhtml when using server controls

    01-05-2009, 7:35 PM
    • Member
      40 point Member
    • run4it
    • Member since 12-27-2002, 1:29 PM
    • Olympia WA, USA
    • Posts 257

     I'm having a heck of a time figuring out how to write code that uses server controls such as asp:panel and asp:literal and renders valid xhtml.  I'm hoping someone can give me some pointers, or tell me it's not possible, and to just quit trying. Here is the problem I'm working on.

    At this website, http://checkoutacollege.com/ReadySetGo/Ready/Ready.aspx, the short list item menu that includes the "Email" and the "Print" links is a user control.  Some pages have both links, some only have one.  The links are displayed or not using attributes set to true or false like this:

    <uc2:ucEmailContent id="ucEmailThisPage" runat="server" EmailVisible="true" PrintVisible="true"  ></uc2:ucEmailContent> .

    The .ascx page for the user control  creates an <ul> with two <li> like this:

    <ul>               
                    <li>
                        <asp:Panel ID="pnlEmailThisPage" runat="server">                 
                        <asp:LinkButton ID="lnkBtnEmailThisPage" runat="server" OnClick="lnkBtnEmailThisPage_Click" Text="Email">
                        </asp:LinkButton>                   
                        &nbsp;&nbsp;|&nbsp;&nbsp;
                        </asp:Panel>
                    </li>
                    <li>
                        <asp:Panel ID="pnlPrintThisPage" runat="server">
                        <a href="javascript:window.print()">                  
                       <asp:Literal ID="litPrint" runat="server" Text="Print"></asp:Literal></a>
                       </asp:Panel>
                    </li>
            </ul>

     

    The problem with this, is that if the Email or the Print link is not set to be visible on a particular page, then it renders as an empty <li>.

    I tried putting the <li> in an <asp:literal> where I could control its visibility or not, but I got an error message that I can't have "text" (the <li> written with the literal) inside of a <ul>.

    I can put the <li> inside the asp:panel, but the use of the asp:panel itself is a problem since it renders as a <div> inside of the <ul> which isn't valid either.

    Anyone have any ideas how I can approach this so that it will render valid code?  Is it possible?  

     

  • Re: how to write valid xhtml when using server controls

    01-06-2009, 11:50 AM
    Answer
    • Contributor
      4,209 point Contributor
    • SyntaxC4
    • Member since 07-13-2007, 2:25 PM
    • Guelph, Ontario
    • Posts 692

    Hi run4it,

    Have you thought about creating a control that inherits the Control class and writing your own render method?  Alternatively there are control adapters, but i've only found examples of creating a xhtml friendly menu, or gridview.

    Here's a post: http://weblogs.asp.net/scottgu/archive/2006/10/28/CSS-Control-Adapters-Update-_2800_Beta3_2900_.aspx

    You might be able to create your own control adapter to achieve the effect you are looking for.

    Here's a post on creating a control adapter: http://weblogs.asp.net/scottgu/archive/2006/05/02/444850.aspx

    Hope this helps.

    *** REMINDER ***
    If you find this post useful, Please click the 'Mark as Answer' Button.

    *** DISCLAIMER ***
    All Code is provided AS IS.
  • Re: how to write valid xhtml when using server controls

    01-08-2009, 7:53 AM
    Answer
    • All-Star
      65,332 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,708
    • TrustedFriends-MVPs

     >Anyone have any ideas how I can approach this so that it will render valid code?  Is it possible? 

    Whilst not applicable in your case as you have a control response on the OnClick event, you could do the following:

    • Make up an HTML sample that passes XHTML validation
    • Generate this (using System.Text.StringBuilder if incremental concatenation is involved)
    • Assign the generated string to a placeholder control
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
    See the FAQ on the correct forum to post at, at http://forums.asp.net/p/1337412/2699239.aspx#2699239
Page 1 of 1 (3 items)