Prevent ASPX form from being renamed

Go To StackoverFlow.com

0

I have an ASPX page, which contains a form. That form has been given a name and and id. The aspx page also has a master page to which it is linked.

When the page is served up, both the name and id of the form are different. The name, for example, changes from "uploadForm" to "Form1"

The action of the form also appears to be altered.

What is the cause of this, and how can I prevent it?

The opening line of the aspx page is thus:

<%@ Page Language="C#" MasterPageFile="~/Modal.Master" AutoEventWireup="true" CodeBehind="UploadPage.aspx.cs" Inherits="WebConnect.UploadPage" %>

And the form tag is:

<form id="uploadForm" name="uploadForm" enctype="multipart/form-data" action="Upload.ashx" method="post">

When served, it appears as:

<form id="Form1" action="UploadPage.aspx" method="post">


UPDATE: Here is the entire body of the page:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <form id="uploadForm" name="uploadForm" enctype="multipart/form-data" action="Upload.ashx" method="post">
        <div>
            File:
            <input id="file1" name="fileField" type="file" />
            <input id="objectID" name="objectID" type="hidden" />
            <input id="fieldID" name="fieldID" type="hidden" />
            <input id="submitType" name="submitType" type="hidden" />
            <progress id="uploadProgress" value="10" max="100" display="none"></progress>
            <button id="Html5Submit" type="button" onclick="AsyncSubmit()">Submit</button>
            <button id="Html4Submit" type="button" onclick= "SyncSubmit()">Submit</button>
        </div>
    </form>
    <div id="Html5Upload">
    </div>
</asp:Content>
2012-04-04 22:40
by Jordaan Mylonas
Are you sure what you're seeing is not a cached page - dee-see 2012-04-04 22:45
I've just tested this and it worked fine for me. ASPX based on MasterPage. MasterPage with form runat="server" and ASPX with your form tag. Form was not renamed, so, as Vache mentions, caching may be at fault here. Framework 3.5, VS 201 - dash 2012-04-04 22:53
@dash it depends, it works only if the upload form is outside the form - Adrian Iftode 2012-04-04 22:59
You are allowed to nest forms as long as uploadForm isn't a server side control (i.e. has no runat="server") - I've just tried it! You can have as many forms as you want, but only one with runat="server". I don't think it's the right thing to do (and I wonder if it's even w3c compliant HTML) but it does seem to work - dash 2012-04-04 23:09
@dash, see this question http://stackoverflow.com/questions/379610/can-you-nest-html-forms is not about ASP .Net, the browsers don't allow this, they discard(remove) the second for - Adrian Iftode 2012-04-04 23:21
@AdrianIftode - see http://stackoverflow.com/a/1190178/1073107 for another opinion (including the comment). In the words of the famous song, "It's not right, but it's okay!" As a more practical example, I have a site that has the master page form (i.e. the aspnetform). It also has another form nested deep within the structure, provided by a third party, that allows the user to subscribe to their newsletter. It works fine on every browser we've tested with. But I still hate it, as, I agree with you, it feels wrong - dash 2012-04-04 23:27
@AdrianIftode Turns out it's a lot more "fun"! http://anderwald.info/internet/nesting-form-tags-in-xhtml - dash 2012-04-04 23:39
@dash, I had a bug few weeks ago similar to that situation where multiple forms were inside another form (the server one). Sure, the first form couldn't submit, only the rests. There I found how the browser discards the very first child form. The fun in the article posted by you is it has the solution for the OP :), just add an empty or hidden form, what a hack : - Adrian Iftode 2012-04-04 23:46
Not a cached page. Not nested forms - Jordaan Mylonas 2012-04-05 03:32
The form will be nested by virtue of the fact that it's in an ASPX page which in turn is inside the form declared by the Master Page - your ContentPlaceHolder, MainContent, is that inside the form in the Master Page - dash 2012-04-05 08:06


1

Rendered control renaming has been a well known problem in ASP.Net for a long time, and there's not a whole lot you can do to prevent it in WebForms. However, you can modify how your JavaScript is searching for the element by use of the ClientId property on Asp.Net elements.

var formId = '<%= uploadForm.ClientID %>';

Source: How to stop ASP.NET from changing IDs in order to use jQuery

In your JavaScript, just use that variable instead of 'uploadForm' and you're good to go.

2012-04-04 22:49
by Rob Rodi
The problem is, it is not just renaming the form element, it is changing its Action parameter - Jordaan Mylonas 2012-04-05 00:14


1

I think you have nested forms, which is not allowed. The first form you see is the one in MasterPage and the second one, uploadForm, is dropped by browsers (the closing tag of uploadForm becomes the closing tag of Form1).

So you have to move uploadForm outside of the Form1 form.

2012-04-04 22:52
by Adrian Iftode
No nested forms, see my update for the full source - Jordaan Mylonas 2012-04-05 00:14
can you show the content of the master page also - Adrian Iftode 2012-04-05 07:45


0

I think the first question really is what is your intent? I'm trying to follow the comments/answers and I don't think its evident (but I may have missed it).

If you want to submit somewhere else other than postback to the same page (the default web forms behavior):

  • If processing is server side, you can have as many button controls (runat="server") as you need each with its own postbackurl
  • @RobRodi 's answer can be used if you want to do client side (Javascript) processing with server side controls
  • You can use an standard html button (html button, not runat="server") and handle its click event entirely via client script (has nothing to do with form action).
2012-04-05 05:57
by EdSF
Ads