Joakim_
asked on
Preventing the "name" attribute in the FORM tag
How do I prevent the server from creating a "name" attribute in the FORM tag when I use the runat="server" attribute in the FORM tag?
u have piqued my interest... why exactly do you want to not have the name attribute on the form rendered???
ASKER
Because the "name" attribute in the FORM tag is not XHTML 1.0 valid. And don't tell me that I should ignore XHTML 1.0, and rather concentrate on making the document valid HTML 4.0.
ah =)
you can do this by providing a custom writer class that writes everything with the exception of the name attribute of the form.
you can do this by providing a custom writer class that writes everything with the exception of the name attribute of the form.
core VB.NET code
============
Imports System.Web.UI
Imports System.IO
Public Class BasePage
Inherits Page
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit er)
MyBase.Render(New FormWriter(writer.InnerWri ter))
End Sub
End Class
Public NotInheritable Class FormWriter
Inherits HtmlTextWriter
Private isForm As Boolean = False
Public Sub New(ByVal writer As TextWriter)
MyBase.New(writer)
End Sub
Public Overrides Sub WriteBeginTag(ByVal tagName As String)
isForm = (String.Compare(tagName, "form", True) = 0)
MyBase.WriteBeginTag(tagNa me)
End Sub
Public Overloads Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String, ByVal fEncode As Boolean)
If isForm AndAlso String.Compare(name, "name", True) = 0 Then
'do nothing
Else
MyBase.WriteAttribute(name , value, fEncode)
End If
End Sub
Public Overloads Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String)
If isForm AndAlso String.Compare(name, "name", True) = 0 Then
'do nothing
Else
MyBase.WriteAttribute(name , value)
End If
End Sub
End Class
============
Imports System.Web.UI
Imports System.IO
Public Class BasePage
Inherits Page
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit
MyBase.Render(New FormWriter(writer.InnerWri
End Sub
End Class
Public NotInheritable Class FormWriter
Inherits HtmlTextWriter
Private isForm As Boolean = False
Public Sub New(ByVal writer As TextWriter)
MyBase.New(writer)
End Sub
Public Overrides Sub WriteBeginTag(ByVal tagName As String)
isForm = (String.Compare(tagName, "form", True) = 0)
MyBase.WriteBeginTag(tagNa
End Sub
Public Overloads Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String, ByVal fEncode As Boolean)
If isForm AndAlso String.Compare(name, "name", True) = 0 Then
'do nothing
Else
MyBase.WriteAttribute(name
End If
End Sub
Public Overloads Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String)
If isForm AndAlso String.Compare(name, "name", True) = 0 Then
'do nothing
Else
MyBase.WriteAttribute(name
End If
End Sub
End Class
example of a page calling this:
aspx
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm13.aspx .vb" Inherits="b1xml2.ExpertExc hange.VB.W ebForm13"% >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm13</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip t content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="FlowLayout ">
<form id="Form1" method="post" runat="server">
</form>
</body>
</html>
aspx.vb
=====
Public Class WebForm13
Inherits BasePage
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
html output
========
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm13</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip t content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="FlowLayout ">
<form method="post" action="WebForm13.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtNjU0MzcyMTk1Ozs +O1I8yz/lV PZzPVdVP36 Z+1JjKKM=" />
</form>
</body>
</html>
aspx
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm13.aspx
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm13</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="FlowLayout
<form id="Form1" method="post" runat="server">
</form>
</body>
</html>
aspx.vb
=====
Public Class WebForm13
Inherits BasePage
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
html output
========
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm13</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="FlowLayout
<form method="post" action="WebForm13.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtNjU0MzcyMTk1Ozs
</form>
</body>
</html>
by the way, I am all for XHTML.
ASKER
Yes... HTML 4.0 < XHTML 1.0.
That's a nice piece of code you got there, but I forgot to mention that I'm programming C#. Sure I will manage to "convert" it, but I will let the question stay open for some more hours.
That's a nice piece of code you got there, but I forgot to mention that I'm programming C#. Sure I will manage to "convert" it, but I will let the question stay open for some more hours.
lol, i had to convert my code from C# to VB.NET
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
typo:
replace string.Compare(name,"name" ),true)
========================== ====
with string.Compare(name,"name" ,true)
public override void WriteAttribute(string name, string value)
{
if (isForm && string.Compare(name,"name" ,true) == 0)
{
//do nothing
}
else
{
base.WriteAttribute(name,v alue);
}
}
public override void WriteAttribute(string name, string value,bool fEncode)
{
if (isForm && string.Compare(name,"name" ,true) == 0)
{
//do nothing
}
else
{
base.WriteAttribute(name,v alue,fEnco de);
}
}
replace string.Compare(name,"name"
==========================
with string.Compare(name,"name"
public override void WriteAttribute(string name, string value)
{
if (isForm && string.Compare(name,"name"
{
//do nothing
}
else
{
base.WriteAttribute(name,v
}
}
public override void WriteAttribute(string name, string value,bool fEncode)
{
if (isForm && string.Compare(name,"name"
{
//do nothing
}
else
{
base.WriteAttribute(name,v
}
}
ASKER
I suddenly figured out that .NET Framework 2.0 Beta 2 doesn't create that "name" attribute, so everything was fine when I switched to 2.0 Beta 2. But I will save that code for further use; I will maybe need it later.