Page view counter

dropdownlist breaks xhtml validation

Last post 02-09-2007 12:37 PM by Rossoneri. 9 replies.

Sort Posts:

  • dropdownlist breaks xhtml validation

    02-09-2007, 10:09 AM
    • Loading...
    • elwise
    • Joined on 02-09-2007, 3:05 PM
    • Posts 4
    • Points 0

    Hi,

    I have this dropdownlist that breaks xhtml w3c validation because it generates a select tag with a javascript property. this only happens when autopostback is set to true.

    so the the validation error that i get is this:

    Error Line 263 column 86: there is no attribute "language".

    ...PostBack('dropArchive','')" language="javascript" id="dropArchive">

    this is the code.

    this control:

     <asp:dropdownlist id="dropArchive" runat="server" AutoPostBack="True"></asp:dropdownlist>

    generates this html:

    <select name="dropArchive" onchange="__doPostBack('dropArchive','')"
    language="javascript" id="dropArchive">
            <option value="1">Episode 1</option>
            <option value="2">Episode 2</option>
            <option selected="selected" value="3">Episode 3</option>
    </select> 

     
    any idea on how can i stop the dropdownlist to generate the language="javascript" bit? 

     
    would the dropdown work without it? (I guess so?)

     
    Thanks! 

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 10:29 AM
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 682
    • Points 3,469

    have you tried doing something like

    droparchive.attributes.remove("language","javascript")

    in the page_load or page_init event in your codebehind? 

    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 10:39 AM
    • Loading...
    • bpag
    • Joined on 04-07-2006, 4:05 PM
    • Columbus, OH
    • Posts 533
    • Points 3,517

    If you take off the autopostback='true' then ASP.Net won't add the onchange='..' to the dropdown but it also won't postback when you change the value so you'd then have to add the javascript your self to do the postback, which seems a little odd just to pass xhtml validation.

     

    If this post answered your question please remember to 'Mark as Answer'!
  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 10:55 AM
    • Loading...
    • elwise
    • Joined on 02-09-2007, 3:05 PM
    • Posts 4
    • Points 0

    thanks, that was a good idea.

    I tried that, but it only accepts 1 attribute, so I tried:

    dropArchive.Attributes.Remove("javascript");
    dropArchive.Attributes.Remove("language");
    dropArchive.Attributes.Remove("language=\"javascript\"");

     
    and even

    dropArchive.Attributes.Clear();

     

    but it didn't do anything.

     
    any other ideas?
     

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 11:05 AM
    Answer
    • Loading...
    • Rossoneri
    • Joined on 11-09-2006, 3:57 PM
    • O-H I-O
    • Posts 433
    • Points 2,079
    I remember fighting this back in the day. We ended up creating a class that inherited from System.Web.UI.Page, then override the Render Method. Then had our aspx pages inherit from this class

    I don't have the original source for this, but I reflected and here is what was in there
    protected override void Render(HtmlTextWriter output)
    {
    StringWriter writer1 = new StringWriter();
    HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
    base.Render(writer2);
    writer2.Close();
    this.xHtml = writer1.GetStringBuilder().ToString();
    this.xHtml = this.xHtml.Replace("<script language=\"javascript\">", "<script type=\"text/javascript\">");
    this.xHtml = this.xHtml.Replace("<script language=\"javascript\" type=\"text/javascript\">", "<script type=\"text/javascript\">");
    this.xHtml = this.xHtml.Replace("language=\"javascript\"", string.Empty);
    output.Write(this.xHtml);
    }

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 11:22 AM
    • Loading...
    • elwise
    • Joined on 02-09-2007, 3:05 PM
    • Posts 4
    • Points 0

    cool thanks!

    so how does it work?

    I need to paste this method in my page and then call it from pageload?

    also, it also looks like the method is specting  HtmlTextWriter ?

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 11:39 AM
    • Loading...
    • Rossoneri
    • Joined on 11-09-2006, 3:57 PM
    • O-H I-O
    • Posts 433
    • Points 2,079

    You create a new class, call it XHtmlPage for example You set it up like

    public class XHtmlPage : System.Web.UI.Page
    {
        // Paste the Render function above in here... public override void Render
        // You can rename the variables as you wish - Reflector gives them generic names as you see above
    }

    That's it. You will have to add some namespaces to the top of this class or qualify the StringWriter and HtmlTextWriter with their appropriate namespaces

    I also noticed that in the function above, xHtml was declared as a private variable so you can either do that as well or add string xHtml = string.Empty at the top of the Render method

    Then when you setup a Page like Default.aspx, when you view the codebehind you see the class has

    _Default : System.Web.UI.Page

    You change it to

    _Default : XHtmlPage

    And of course if the class you created has a different namespace, then you will have to bring that it as well with a using statement at the top or qualifying it. Does this make sense?

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 11:43 AM
    • Loading...
    • Rossoneri
    • Joined on 11-09-2006, 3:57 PM
    • O-H I-O
    • Posts 433
    • Points 2,079
    To answer your question... you do not have to pass it an HtmlTextWriter. .NET does that internally.
  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 12:25 PM
    • Loading...
    • elwise
    • Joined on 02-09-2007, 3:05 PM
    • Posts 4
    • Points 0

    Thanks man, that worked perfectly!

    now let's hope valencia beats inter next week in the champions league!
     

  • Re: dropdownlist breaks xhtml validation

    02-09-2007, 12:37 PM
    • Loading...
    • Rossoneri
    • Joined on 11-09-2006, 3:57 PM
    • O-H I-O
    • Posts 433
    • Points 2,079
    Glad it worked for you. Don't forget to mark the post that answered your question.

    Valencia is my second favorite team! I hope they embarrass Inter Big Smile
Page 1 of 1 (10 items)