影响另一个提交文件

时间:2012-08-29 10:17:55

标签: c# javascript asp.net forms

我已经在不同的窗口中弹出了一个上传功能,当我提交时,我想将信息发送回上传窗口打开的页面。

使用ASP.net或C#可能有某种方式吗? 或者我必须使用一些JavaScript?怎么样?

我的2页: news.aspx - 包含我的新闻的formview。和一些输入的表格。 这是打开上传页面的链接......

uploader.aspx - 包含我的上传控制器和要上传的C#代码。 这应该将我的C#代码中的字符串发送回news.aspx并将其放在我的输入字段或标签中,这并不重要。

uploader.aspx文件:

<form id="form1" runat="server">
<div>
Vælg en fil at uploade:<br />
    <asp:FileUpLoad id="FileUpLoad1" runat="server" />
    <asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />
    <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>

代码背后:

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

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

}
protected void UploadBtn_Click(object sender, EventArgs e) {

    Label1.Text = "Status: Uploader...";
    if (FileUpLoad1.HasFile) {
        FileUpLoad1.SaveAs(@"C:\Users\138409\Documents\Visual Studio 2010\Projects\Musicon\img\news\" + FileUpLoad1.FileName);
        Label1.Text = "Status: " + FileUpLoad1.FileName + " er blevet uploadet";
    } else {
        Label1.Text = "Status: Filen blev ikke uploadet...";
    }
}
}

1 个答案:

答案 0 :(得分:0)

以下是一些用于创建弹出窗口的Javascript代码,并在弹出窗口关闭之前检索一些信息。 (我稍后会添加一个jQuery示例)

//Site1.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="PopupRedirect.Site1" %>

<!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>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

//Page1.aspx
<%@ Page Title="" Language="C#" MasterPageFile="Site1.Master" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="PopupRedirect.Page1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  <script type="text/javascript">
    var message = "Hello World!";

    var closeWindow = function(event) {
      event.preventDefault();
      window.close();
    };

    window.onload = function() {
      document.getElementById('upload').addEventListener('click', closeWindow, false);
    };
  </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <button id="upload">Upload File</button>
</asp:Content>


//UploadPage.aspx
<%@ Page Title="" Language="C#" MasterPageFile="Site1.Master" AutoEventWireup="true" CodeBehind="UploadPage.aspx.cs" Inherits="PopupRedirect.UploadPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  <script type="text/javascript">
    var popupWindow;
    var windowOptions = "menubar=yes,location=yes,resizable=no,scrollbars=no,status=yes,width=350,height=350";
    var upload = function (event) {
      event.preventDefault();

      popupWindow = window.open("Page1.aspx", "Upload Page", windowOptions);
      popupWindow.onbeforeunload = pageClose;
    };

    var pageLoad = function(event) {
      document.getElementById('uploadLink').addEventListener('click', upload, false);
    };

    var pageClose = function (event) {
      //put the code here that you want to execute when the window is done.
      //like getting the value of some javascript variables
      if(typeof (popupWindow.message) != "undefined") {
        alert(popupWindow.message);
      }
    };

    window.onload = pageLoad;

  </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <span>Welcome to this page! Click to upload.</span>
  <button id="uploadLink">Upload</button>
</asp:Content>