0

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

用 ASP.net 或 C# 有可能吗?或者我必须使用一些 javascript 吗?如何?

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

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...";
    }
}
}
4

1 回答 1

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>
于 2012-08-29T12:56:16.037 回答