Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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????

Grant me the serenity to accept the things I cannot change, the courage to change the things I cannot accept and the wisdom to hide the bodies of those people I had to kill today cos they pi**ed me off.
  • 1 year later...
Posted

[PLAIN]<asp:imagebutton rendering a border="0" causing page not to be w3c validated[/PLAIN]

 

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/ProductFeedback/viewfeedback.aspx?feedbackid=ecb2b662-14aa-41d9-9af2-f98259836a70

Posted

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

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

<%@ 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


<!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>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...