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

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

Post by j.madgwic » Fri, 18 Feb 2005 19:34:10


Hi,
I am trying to get my page validated on the W3C's "Markup Validation
Service" ( http://www.yqcomputer.com/ ) 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.
 
 
 

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

Post by recoi » Sat, 19 Feb 2005 01:37:56


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.

 
 
 

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

Post by Ken Cox [M » Sat, 19 Feb 2005 03:36:57

Is there anything in this article that helps with that?

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

http://www.yqcomputer.com/
 
 
 

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

Post by j.madgwic » Sat, 19 Feb 2005 19:01:16


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.
 
 
 

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

Post by j.madgwic » Sat, 19 Feb 2005 19:04:44


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.
 
 
 

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

Post by j.madgwic » Sat, 19 Feb 2005 23:56:02

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[i].Index + mcTags[i].Value.IndexOf(sAttrName) - 1;
nLength = mcTags[i].Length - mcTags[i].Value.IndexOf(sAttrName) + 1;
sHTMLoutput = sHTMLoutput.Remove(nStart, nLength);
}
}
 
 
 

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

Post by recoi » Sun, 20 Feb 2005 06:27:10

Glad it could be of help