Search code examples
c#asp.netasp.net-ajaxajaxcontroltoolkitasyncfileupload

Parent page including iframe form with AsyncFileUpload. redirect to "_top" after uploading and clicking "send"


I have a parent page (testFrame.aspx) including an iframe.

The iframe is an aspx form (form.aspx) with an AsyncFileUpload control (ajaxcontroltoolkit).
The form contains a textbox and a required field validator.
In the bt_Send Codebehind, I check if the user entered a text in the text box and if not I show an error message.

Now, if I test my parent form and I enter a text and click "send" I see my feedback message (the text I entered in the textbox).
If I don't enter a text, and I click send, I get the error message.

The weird thing happens now: if I don't enter a text but I upload a file with the AsyncFileUpload.
I click "send" and instead of getting the error message, I'm redirected to form.aspx outside my parent page (like target="_top"), without error message. I just see the clean form not anymore in my iframe.

I don't want that. I want the form to stay inside my parent page

testForm.aspx looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    This is a container page. The following is inside an iframe:
    <br />

    <iframe src="form.aspx" width="800px" height="400px"></iframe>
    </div>
    </form>
</body>
</html>

form.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="form.aspx.cs" Inherits="TestToolkit.form" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test Form</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>&nbsp;&nbsp;
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

        <br /><br />
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        Upload picture: <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" ClientIDMode="AutoID" />

            <div id="uploadOk" style="position: relative; left: 330px; bottom: 40px; width:0px; height:0px; display: none;"><asp:Image ID="imgOk" ImageUrl="_img/check-mark-40.png" runat="server" style="" AlternateText="Erledigt!" /></div>

        <br /><br />
        <asp:Button ID="btSend" runat="server" onclick="btSend_Click" Text="Send" />
        <br /><br />
        <asp:Label ID="lblFeedback" runat="server"></asp:Label>
&nbsp;<asp:RequiredFieldValidator runat="server" ID="rfvFirstName" ControlToValidate="txtFirstName" Display="None" EnableClientScript="false" />
    </div>
    </form>
</body>
</html>

and the code behind form.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestToolkit
{
    public partial class form : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btSend_Click(object sender, EventArgs e)
        {
            //ShowOrRemoveErrors();

            if (IsValid)
            {
                lblFeedback.Text = txtFirstName.Text;
            }
            else
            {
                lblFeedback.Text = "Please enter your name";
            }
        }
    }
}

Solution

  • Well I guess I found the answer to my question.

    The problem comes if a file was uploaded AND the fields are not valid (or required fields have no text).

    The problem comes on postback. So what I did is to implement a client side validation using EnableClientScript="true":

    <asp:RequiredFieldValidator runat="server" ID="rfvFirstName" ControlToValidate="txtFirstName" Display="None" EnableClientScript="true" />
    

    With client side validation there is no postback unless all my required fields are filled correctly.

    Hope this helps people with my same problem.