如题,我在网上找了半天,找是找了不少,但还是不会操作,以前用的是asp做网页的,那里的用的input的name属性,window.opener.document.formname.inputname.value=值就可以了。但是.net中TextBox没有name属性,再加上用window.open方法弹口的窗口容易被禁止,所以希望哪位朋友给一个用window.showModalDialog("url",obj,"")这种方法传值的实例。请发到,最好是带一个参数的,因为一个页面可能有好几个地方调用这个文件,这样,我只要在按钮上加一个参数,就传到相应的那个框中,谢谢。
最近发了好几个贴子,分数给的高,但是都没有什么人给出满意的答案,所以这里只放10分,解决问题后加100分.
下面是我写的代码,如果不想全部重写的话,你可以从这里面复制一部分代码,文件能正常上传,但是不能正确的传给父窗口。
父窗口HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="putfile.aspx.cs" Inherits="systemadmin_product_putfile" %><!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>
<script language="javascript" type="text/javascript">
function opendialog1(obj)
{
var someValue=window.showModalDialog("../upfile.aspx?editname="+obj,"","dialogWidth=300px;dialogHeight=150px;status=no;help=no;scrollbars=no");
document.getElementById(obj).value=someValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="pic" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="上传" /></div>
</form>
</body>
</html>父窗口cs代码:
protected void Page_Load(object sender, EventArgs e)
{
this.Button1.Attributes.Add("onclick", "Javascript:opendialog1('pic')");
}子窗口HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="systemadmin_upfile" %><!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>
<link rel="stylesheet" href="acss.css" />
<base target="_self" />
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<input id="hiddenfile" runat="server" style="width: 205px" type="hidden" visible="false" />
<asp:TextBox ID="editname" runat="server" Visible="False"></asp:TextBox><br />
<input id="upfile" type="file" runat="server" />
<br />
<br />
<asp:Label ID="ErrMsg" runat="server" ForeColor="Red" Height="16px" Text="选择要上传的文件"
Width="218px"></asp:Label><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" /></div>
</form>
</body>
</html>子窗口cs代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class systemadmin_upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string editname = this.editname.Text.Trim().ToString();
string filename = this.upfile.PostedFile.FileName.Trim().ToString();
if (filename != "")
{
string fileflx = filename.Substring(filename.LastIndexOf(".")).ToLower().ToString();
if (fileflx == ".jpg" || fileflx == ".gif" || fileflx=="bmp")
{
Random rnd = new Random();
string fname = DateTime.Now.ToString("yyyymmddhhmmssffff") + rnd.Next(10, 99);
//string filepath =".."+"\\"+ Server.MapPath("upfile") + "\\" + fname + fileflx;
string filepath = Server.MapPath("upfile") + "\\" + fname + fileflx;
//Response.Write(filepath);
this.upfile.PostedFile.SaveAs(filepath);
//this.hiddenfile.Value = filepath;
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>window.opener.document.getElementById('" + editname + "').value='" + filepath + "';alert('上传文件成功');window.close();</script>");
//Response.Write("<script>alert('上传成功');parent.window.returnValue='"+filepath+"';window.close()</script>");
}
else
{
this.ErrMsg.Text = "你只能上传JPG和GIF文件";
}
}
else
{
this.ErrMsg.Text = "请选择要上传的文件";
}
}
}
最近发了好几个贴子,分数给的高,但是都没有什么人给出满意的答案,所以这里只放10分,解决问题后加100分.
下面是我写的代码,如果不想全部重写的话,你可以从这里面复制一部分代码,文件能正常上传,但是不能正确的传给父窗口。
父窗口HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="putfile.aspx.cs" Inherits="systemadmin_product_putfile" %><!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>
<script language="javascript" type="text/javascript">
function opendialog1(obj)
{
var someValue=window.showModalDialog("../upfile.aspx?editname="+obj,"","dialogWidth=300px;dialogHeight=150px;status=no;help=no;scrollbars=no");
document.getElementById(obj).value=someValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="pic" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="上传" /></div>
</form>
</body>
</html>父窗口cs代码:
protected void Page_Load(object sender, EventArgs e)
{
this.Button1.Attributes.Add("onclick", "Javascript:opendialog1('pic')");
}子窗口HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="systemadmin_upfile" %><!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>
<link rel="stylesheet" href="acss.css" />
<base target="_self" />
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<input id="hiddenfile" runat="server" style="width: 205px" type="hidden" visible="false" />
<asp:TextBox ID="editname" runat="server" Visible="False"></asp:TextBox><br />
<input id="upfile" type="file" runat="server" />
<br />
<br />
<asp:Label ID="ErrMsg" runat="server" ForeColor="Red" Height="16px" Text="选择要上传的文件"
Width="218px"></asp:Label><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" /></div>
</form>
</body>
</html>子窗口cs代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class systemadmin_upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string editname = this.editname.Text.Trim().ToString();
string filename = this.upfile.PostedFile.FileName.Trim().ToString();
if (filename != "")
{
string fileflx = filename.Substring(filename.LastIndexOf(".")).ToLower().ToString();
if (fileflx == ".jpg" || fileflx == ".gif" || fileflx=="bmp")
{
Random rnd = new Random();
string fname = DateTime.Now.ToString("yyyymmddhhmmssffff") + rnd.Next(10, 99);
//string filepath =".."+"\\"+ Server.MapPath("upfile") + "\\" + fname + fileflx;
string filepath = Server.MapPath("upfile") + "\\" + fname + fileflx;
//Response.Write(filepath);
this.upfile.PostedFile.SaveAs(filepath);
//this.hiddenfile.Value = filepath;
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>window.opener.document.getElementById('" + editname + "').value='" + filepath + "';alert('上传文件成功');window.close();</script>");
//Response.Write("<script>alert('上传成功');parent.window.returnValue='"+filepath+"';window.close()</script>");
}
else
{
this.ErrMsg.Text = "你只能上传JPG和GIF文件";
}
}
else
{
this.ErrMsg.Text = "请选择要上传的文件";
}
}
}
function opendialog1(obj)
{
var someValue=window.showModalDialog("../upfile.aspx?editname="+obj,"","dialogWidth=300px;dialogHeight=150px;status=no;help=no;scrollbars=no");
document.getElementById(obj).value=someValue;
}
这句改成:
function opendialog1(obj)
{
var someValue=window.showModalDialog("../upfile.aspx?editname="+obj,window,"dialogWidth=300px;dialogHeight=150px;status=no;help=no;scrollbars=no");
document.getElementById(obj).value=someValue;
}
然后,在子窗体里的JS中
var obj=window.dialogArguments;
obj.document.getElementById('" + editname + "').value='" + filepath + "';alert('上传文件成功');window.close();
这个贴子我结了。