Link to home
Start Free TrialLog in
Avatar of Joakim_
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?
Avatar of b1xml2
b1xml2
Flag of Australia image

u have piqued my interest... why exactly do you want to not have the name attribute on the form rendered???
Avatar of Joakim_
Joakim_

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.
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.HtmlTextWriter)
        MyBase.Render(New FormWriter(writer.InnerWriter))
    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(tagName)
    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
example of a page calling this:

aspx
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm13.aspx.vb" Inherits="b1xml2.ExpertExchange.VB.WebForm13"%>
<!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_defaultClientScript 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.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration 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_defaultClientScript 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/lVPZzPVdVP36Z+1JjKKM=" />


    </form>

  </body>
</html>
by the way, I am all for XHTML.
Avatar of Joakim_

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.
lol, i had to convert my code from C# to VB.NET
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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,value);
          }
     }
     
     public override void WriteAttribute(string name, string value,bool fEncode)
     {
          if (isForm && string.Compare(name,"name",true) == 0)
          {
               //do nothing
          }
          else
          {
               base.WriteAttribute(name,value,fEncode);
          }
     }
Avatar of Joakim_

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.