How do I remove attributes that are automatically generated by asp.net

Discussion in 'ASP .Net Web Controls' started by John Madgwick, Feb 17, 2005.

  1. Hi,
    I am trying to get my page validated on the W3C's "Markup Validation
    Service" (http://validator.w3.org) but I am having a problem with the
    HTML generated by the button control.

    ASPX code:
    <asp:button id="btnSubmit" runat="server" Text="Submit
    Form"></asp:button>

    Rendered HTML:
    <input type="submit" name="btnSubmit" value="Submit Form" onclick="if
    (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
    language="javascript" id="btnSubmit" />

    The problem is that lanaguage="javascript" is not a valid attribute
    for the <input> tag in HTML 4.01.
    I have tried removing it in my code behind page using
    btnSubmit.Attributes.Remove("lanaguage"), but language does not appear
    as part of the AttributeCollection.

    Sven Groot had exactly the same problem back in 2002 ("Removing
    non-standards-compliant attribute from rendered HTML") but no-one
    posted a reply to his problem.

    Is there any way of removing/excluding attributes that are
    automatically generated by asp.net?
    Is anyone else having difficulties getting their ASPX pages validated
    to the W3C standards?

    Cheers,
    John.
     
    John Madgwick, Feb 17, 2005
    #1
    1. Advertising

  2. John Madgwick

    Guest Guest

    I am not sure where the language="javascript" is coming from. I do not
    believe i have ever seen that. Maybe it is an affter effect of Microsofts
    validator controls.
    I believe the proper place to look for solving this would be in the Page's
    Render event.
    Don't have time to elaborate upon the exact details but basically Render is
    where the asp.net DOM is turned into HTML. therefore anything you wish to be
    done needs to be done before or after base.render has been called. I am
    thinking in this case that it has to be done after calling base.render.

    "John Madgwick" <> wrote in message
    news:...
    > Hi,
    > I am trying to get my page validated on the W3C's "Markup Validation
    > Service" (http://validator.w3.org) but I am having a problem with the
    > HTML generated by the button control.
    >
    > ASPX code:
    > <asp:button id="btnSubmit" runat="server" Text="Submit
    > Form"></asp:button>
    >
    > Rendered HTML:
    > <input type="submit" name="btnSubmit" value="Submit Form" onclick="if
    > (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
    > language="javascript" id="btnSubmit" />
    >
    > The problem is that lanaguage="javascript" is not a valid attribute
    > for the <input> tag in HTML 4.01.
    > I have tried removing it in my code behind page using
    > btnSubmit.Attributes.Remove("lanaguage"), but language does not appear
    > as part of the AttributeCollection.
    >
    > Sven Groot had exactly the same problem back in 2002 ("Removing
    > non-standards-compliant attribute from rendered HTML") but no-one
    > posted a reply to his problem.
    >
    > Is there any way of removing/excluding attributes that are
    > automatically generated by asp.net?
    > Is anyone else having difficulties getting their ASPX pages validated
    > to the W3C standards?
    >
    > Cheers,
    > John.
     
    Guest, Feb 17, 2005
    #2
    1. Advertising

  3. Is there anything in this article that helps with that?

    A C# class to make your ASP.NET pages XHTML valid

    http://www.codeproject.com/aspnet/ASPNET2XHTML.asp


    "John Madgwick" <> wrote in message
    news:...
    > Hi,
    > I am trying to get my page validated on the W3C's "Markup Validation
    > Service" (http://validator.w3.org) but I am having a problem with the
    > HTML generated by the button control.
    >
    > ASPX code:
    > <asp:button id="btnSubmit" runat="server" Text="Submit
    > Form"></asp:button>
    >
    > Rendered HTML:
    > <input type="submit" name="btnSubmit" value="Submit Form" onclick="if
    > (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
    > language="javascript" id="btnSubmit" />
    >
    > The problem is that lanaguage="javascript" is not a valid attribute
    > for the <input> tag in HTML 4.01.
    > I have tried removing it in my code behind page using
    > btnSubmit.Attributes.Remove("lanaguage"), but language does not appear
    > as part of the AttributeCollection.
    >
    > Sven Groot had exactly the same problem back in 2002 ("Removing
    > non-standards-compliant attribute from rendered HTML") but no-one
    > posted a reply to his problem.
    >
    > Is there any way of removing/excluding attributes that are
    > automatically generated by asp.net?
    > Is anyone else having difficulties getting their ASPX pages validated
    > to the W3C standards?
    >
    > Cheers,
    > John.
     
    Ken Cox [Microsoft MVP], Feb 17, 2005
    #3
  4. <> wrote in message news:<u#$>...
    > I am not sure where the language="javascript" is coming from. I do not
    > believe i have ever seen that. Maybe it is an affter effect of Microsofts
    > validator controls.
    > I believe the proper place to look for solving this would be in the Page's
    > Render event.
    > Don't have time to elaborate upon the exact details but basically Render is
    > where the asp.net DOM is turned into HTML. therefore anything you wish to be
    > done needs to be done before or after base.render has been called. I am
    > thinking in this case that it has to be done after calling base.render.


    You are right. The 'language="javascript"' attribute only appears in
    the input tag if there is a validation control on the page, and there
    are lots of validation controls on my page!

    Thanks for the pointer. I'm looking into it now.
     
    John Madgwick, Feb 18, 2005
    #4
  5. "Ken Cox [Microsoft MVP]" <> wrote in message news:<OXsBq#>...
    > Is there anything in this article that helps with that?
    >
    > A C# class to make your ASP.NET pages XHTML valid
    >
    > http://www.codeproject.com/aspnet/ASPNET2XHTML.asp


    Thanks Ken, thats a good page. Unfortunately, the script doesn't
    actually remove the 'language="javascript"' attribute from the input
    tag, but it has put me on the right tracks.
     
    John Madgwick, Feb 18, 2005
    #5
  6. Ok, I have got a solution to my problem. Thanks to Ken and Recoil for
    their direction.
    Here's my solution in c#:

    using System.IO;
    using System.Text.RegularExpressions;
    protected string sHTMLoutput = "";
    /// <summary>
    /// Intercept the HTML generated by asp.net before it is sent to the
    /// browser and make any required changes to it.
    /// </summary>
    protected override void Render(HtmlTextWriter output)
    {
    StringWriter w = new StringWriter();

    HtmlTextWriter myoutput = new HtmlTextWriter(w);
    base.Render(myoutput);
    myoutput.Close();

    sHTMLoutput = w.GetStringBuilder().ToString();
    // Remove the language attribute from all input tags
    RemoveAttribute("input", "language");

    output.Write(sHTMLoutput);
    }
    /// <summary>
    /// Removes the passed Attribute from all specified HTML Tags
    /// in the sHTMLoutput string.
    /// </summary>
    /// <param name="sTagName">HTML that the Attribute belongs to.</param>
    /// <param name="sAttrName">HTML Attribute to be removed</param>
    private void RemoveAttribute(string sTagName, string sAttrName)
    {
    int nStart = 0;
    int nLength = 0;

    // Matches the tag containing the attribute
    Regex rTagWithAttr = new Regex("<"+ sTagName +"[^>]* "+ sAttrName
    +"=\"(.*?)\"");
    // Collection contain all occurances of the tag with the attribute
    MatchCollection mcTags = rTagWithAttr.Matches(sHTMLoutput);
    // Count BACKWARDS through the collection because the sHTMLoutput
    // length is affected each time an attribute is removed
    for (int i = mcTags.Count-1; i >= 0; i--)
    {
    nStart = mcTags.Index + mcTags.Value.IndexOf(sAttrName) - 1;
    nLength = mcTags.Length - mcTags.Value.IndexOf(sAttrName) + 1;
    sHTMLoutput = sHTMLoutput.Remove(nStart, nLength);
    }
    }
     
    John Madgwick, Feb 18, 2005
    #6
  7. John Madgwick

    Guest Guest

    Glad it could be of help


    "John Madgwick" <> wrote in message
    news:...
    > Ok, I have got a solution to my problem. Thanks to Ken and Recoil for
    > their direction.
    > Here's my solution in c#:
    >
    > using System.IO;
    > using System.Text.RegularExpressions;
    > protected string sHTMLoutput = "";
    > /// <summary>
    > /// Intercept the HTML generated by asp.net before it is sent to the
    > /// browser and make any required changes to it.
    > /// </summary>
    > protected override void Render(HtmlTextWriter output)
    > {
    > StringWriter w = new StringWriter();
    >
    > HtmlTextWriter myoutput = new HtmlTextWriter(w);
    > base.Render(myoutput);
    > myoutput.Close();
    >
    > sHTMLoutput = w.GetStringBuilder().ToString();
    > // Remove the language attribute from all input tags
    > RemoveAttribute("input", "language");
    >
    > output.Write(sHTMLoutput);
    > }
    > /// <summary>
    > /// Removes the passed Attribute from all specified HTML Tags
    > /// in the sHTMLoutput string.
    > /// </summary>
    > /// <param name="sTagName">HTML that the Attribute belongs to.</param>
    > /// <param name="sAttrName">HTML Attribute to be removed</param>
    > private void RemoveAttribute(string sTagName, string sAttrName)
    > {
    > int nStart = 0;
    > int nLength = 0;
    >
    > // Matches the tag containing the attribute
    > Regex rTagWithAttr = new Regex("<"+ sTagName +"[^>]* "+ sAttrName
    > +"=\"(.*?)\"");
    > // Collection contain all occurances of the tag with the attribute
    > MatchCollection mcTags = rTagWithAttr.Matches(sHTMLoutput);
    > // Count BACKWARDS through the collection because the sHTMLoutput
    > // length is affected each time an attribute is removed
    > for (int i = mcTags.Count-1; i >= 0; i--)
    > {
    > nStart = mcTags.Index + mcTags.Value.IndexOf(sAttrName) - 1;
    > nLength = mcTags.Length - mcTags.Value.IndexOf(sAttrName) + 1;
    > sHTMLoutput = sHTMLoutput.Remove(nStart, nLength);
    > }
    > }
     
    Guest, Feb 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leszek
    Replies:
    1
    Views:
    379
    Steven Cheng[MSFT]
    Mar 5, 2004
  2. JezB
    Replies:
    3
    Views:
    1,821
  3. Replies:
    1
    Views:
    1,499
  4. John
    Replies:
    1
    Views:
    320
    David Golightly
    Nov 22, 2006
  5. puzzlecracker
    Replies:
    3
    Views:
    502
    Pete Becker
    Dec 26, 2005
Loading...

Share This Page