?
Solved

Preventing the "name" attribute in the FORM tag

Posted on 2005-05-03
11
Medium Priority
?
615 Views
Last Modified: 2013-11-19
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?
0
Comment
Question by:Joakim_
  • 8
  • 3
11 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13917644
u have piqued my interest... why exactly do you want to not have the name attribute on the form rendered???
0
 
LVL 2

Author Comment

by:Joakim_
ID: 13918271
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.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13918365
ah =)

you can do this by providing a custom writer class that writes everything with the exception of the name attribute of the form.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Expert Comment

by:b1xml2
ID: 13918490
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
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13918496
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>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13919916
by the way, I am all for XHTML.
0
 
LVL 2

Author Comment

by:Joakim_
ID: 13922128
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.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13923238
lol, i had to convert my code from C# to VB.NET
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 400 total points
ID: 13923286
using System;
using System.Web.UI;
using System.IO;

public class BasePage : Page
{
      protected override void Render(HtmlTextWriter writer)
      {
            base.Render(new FormWriter(writer.InnerWriter));
      }
}

public sealed class FormWriter : HtmlTextWriter
{
      private bool isForm = false;
            
      public FormWriter(TextWriter writer) : base(writer) { }
      
      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);
            }
      }
      
      public override void WriteBeginTag(string tagName)
      {
            isForm = string.Compare(tagName,"form",true) == 0;
            base.WriteBeginTag(tagName);
      }
      
}
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13923290
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);
          }
     }
0
 
LVL 2

Author Comment

by:Joakim_
ID: 13927953
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question