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

J

John Madgwick

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.
 
G

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.
 
J

John Madgwick

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.
 
J

John Madgwick

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);
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top