HTML validation from W3C

bizzydint

Regular
Joined
Apr 8, 2003
Messages
75
I'm attempting to get the site to have valid HTML - this is causing much grief due to several reasons (most of which arent .net related so i wont ask about them here)

The one that *is* related is that the built in .net controls, eg an image button is writing out html with attributes which are not accepted by the HTML validator (http://validator.w3.org).


My code reads:
<asp:imagebutton id="btnLogin" onclick="doLogin" tabIndex="3" Runat="server" imageurl="enterButton.gif" width="45" height="18" EnableViewState="False"></asp:imagebutton>


The outputted HTML is:
<input type="image" name="btnLogin" id="btnLogin" tabindex="3" src="enterButton.gif" border="0" style="height:18px;width:45px;" />


Apparently there is no "border" attribute of the INPUT tag. Is there a way to stop this????
 
<asp:imagebutton rendering a [I]border="0"[/I] causing page not to be w3c validated

Hi,
I have same problem with <asp:imagebutton rendering a border="0" attribute...
I wrote to Microsoft reporting the bug at MSDN product Feedback Center (ASP.NET 2.0)...
I suppose somebody joining my notice could push Microsoft for a quicker solution.
Please find my bug report at
http://lab.msdn.microsoft.com/Produ...edbackid=ecb2b662-14aa-41d9-9af2-f98259836a70
 
I would try deriving from the ImageButton class and overriding the Render method. When you call the base class's Render method, pass in a new instance of a HtmlWriter and then remove the border attribute using regex, and writing that data to the HtmlWriter that was passed to the derived ImageButton class.

E.x.:
NoBorderImageButton
Code:
using System;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace WebApplication2
{
	public class NoBorderImageButton : System.Web.UI.WebControls.ImageButton
	{
		protected override void Render(HtmlTextWriter writer)
		{
			//create a temporary HtmlTextWriter
			using(StringWriter htmlOutput = new StringWriter())
			using(HtmlTextWriter tempWriter = new HtmlTextWriter(htmlOutput))
			{
				//get the html
				base.Render(tempWriter);
				tempWriter.Flush();
				string html = htmlOutput.ToString();

				//rewrite the html stripping the border property
				html = Regex.Replace(html, "border\\s*=\\s*((\\\"\\d+\\\")|(\\d+))", "");

				//write the stripped html
				writer.Write(html);
			}
		}
	}
}

Test aspx page
Code:
<%@ Register TagPrefix="myControls" Namespace="WebApplication2" Assembly="WebApplication2" %>
<%@ Page language="c#" EnableViewState="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>WebForm1</title>
  </HEAD>	
    <form id="Form1" method="post" runat="server">
<asp:ImageButton id="aspNetImgBtn" runat="server" AlternateText="asp:ImageButton" ImageUrl="http://xtremedotnettalk.com/image.php?u=22970&dateline=1100314258"></asp:ImageButton>
<br>
<myControls:NoBorderImageButton id="myCustomImgBtn" AlternateText="myControls:NoBorderImageButton" ImageUrl = "http://xtremedotnettalk.com/image.php?u=22970&dateline=1100314258" runat="server"></myControls:NoBorderImageButton>

    </form>
	
  </body>
</HTML>

Rendered Html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>WebForm1</title>
  </HEAD>	
    <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDw2NzE3MDk5Nzs7bDxhc3BOZXRJbWdCdG47bXlDdXN0b21JbWdCdG47Pj63uCkHDwmE/vMwiv2m5xFyR8kipw==" />

<input type="image" name="aspNetImgBtn" id="aspNetImgBtn" src="http://xtremedotnettalk.com/image.php?u=22970&dateline=1100314258" alt="asp:ImageButton" border="0" />
<br>
<input type="image" name="myCustomImgBtn" id="myCustomImgBtn" src="http://xtremedotnettalk.com/image.php?u=22970&dateline=1100314258" alt="myControls:NoBorderImageButton"  />

    </form>
	
  </body>
</HTML>
 
Back
Top