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>
Jordaan Mylonas
  • 1,261
  • 1
  • 11
  • 24
  • Are you sure what you're seeing is not a cached page? – dee-see Apr 04 '12 at 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 2010 – dash Apr 04 '12 at 22:53
  • @dash it depends, it works only if the upload form is outside the form1 – Adrian Iftode Apr 04 '12 at 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 Apr 04 '12 at 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 form – Adrian Iftode Apr 04 '12 at 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 Apr 04 '12 at 23:27
  • @AdrianIftode Turns out it's a lot more "fun"! http://anderwald.info/internet/nesting-form-tags-in-xhtml/ – dash Apr 04 '12 at 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 Apr 04 '12 at 23:46
  • Not a cached page. Not nested forms. – Jordaan Mylonas Apr 05 '12 at 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 Apr 05 '12 at 08:06

3 Answers3

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.

Community
  • 1
  • 1
Rob Rodi
  • 3,476
  • 20
  • 19
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.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
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).
EdSF
  • 11,753
  • 6
  • 42
  • 83