Javascript 如何从代码隐藏访问IFRAME标记内的图像?
我有Frm_MngAddGoodsGrp.aspx页面在这个页面中有一个iframe标记,它指向一个页面“OpenDialog.aspx”Javascript 如何从代码隐藏访问IFRAME标记内的图像?,javascript,asp.net,image,iframe,file-upload,Javascript,Asp.net,Image,Iframe,File Upload,我有Frm_MngAddGoodsGrp.aspx页面在这个页面中有一个iframe标记,它指向一个页面“OpenDialog.aspx”这个页面在我的项目中,我创建它是为了打开一个对话框文件并向用户预览一个图像。我使用名为“ImageRequestHandler.ashx”的httphandler文件通过更改图像控件“ImgPrv”的src属性来预览图像对于“ImageRequestHandler.ashx”,这很好,用户可以在提交保存按钮之前打开一个文件并预览它我还提到,我使用了一个会话将图
这个页面在我的项目中,我创建它是为了打开一个对话框文件并向用户预览一个图像。我使用名为“ImageRequestHandler.ashx”的httphandler文件通过更改图像控件“ImgPrv”的src属性来预览图像对于“ImageRequestHandler.ashx”,这很好,用户可以在提交保存按钮之前打开一个文件并预览它
我还提到,我使用了一个会话将图像文件的数据存储在字节数组中,并将此字节数组保存到数据库中。这些都是用户希望向数据库插入新数据的时间。
但我的问题是,用户希望看到以前存储在db中的数据的时间
我在Frm_MngAddGoodsGrp.aspx中有一个radgrid,我希望当用户单击一行radgrid时,IFRAME中的图像会从db更改为相应的图像,
我可以从Db读取图像的字节数组,但我不知道如何将其设置为图像,我还可以设置httphandler使用的会话变量,但我不知道如何在Iframe标记内设置图像的imageUrl 我在页面中也使用了一个简单的图像控件,但我想在iframe中更改图像是否可能>如果可能,我如何做 在我的aspx文件Frm_MngAddGoodsGrp.aspx中
<iframe id ="OpenDialogControl" runat="server"
src = "OpenDialog.aspx" frameborder="0" name="Iframe1"
scrolling="no" height="110px" width="100px"></iframe>
byte[] SelectedImage;
SelectedImage = (byte[])(ImageArray.Rows[selectedReceiptIndex][8]); //Image Array ->Grid Data table
Session["SessionImage"] = SelectedImage;
Random random = new Random();
ShowImage0.ImageUrl = Page.ResolveClientUrl("~/ImageRequestHandler.ashx?randomno="+ random.Next(0,1000).ToString());
//I have tried following codes to access ImagePrv Element in Iframe1 but i couldn't
//var image = OpenDialogControl.FindControl("ImagePrv") as Image;
在我的aspx文件打开对话框中
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" >
function getRandomNumber() {
var randomnumber = Math.random(10000);
return randomnumber;
}
function OnClientAsyncFileUploadComplete(sender, args) {
var handlerPage = '<%= Page.ResolveClientUrl("~/ImageRequestHandler.ashx")%>';
var queryString = '?randomno=' + getRandomNumber();
var src = handlerPage + queryString;
var clientId = 'ImagePrv';
document.getElementById(clientId).setAttribute("src", src);
}
function showName(object) {
document.write(object.id);
}
</script>
</head>
<body style= "margin-top:0px; margin-left:0px; padding-left:0px; padding-top:0px; ">
<form id="form1" runat="server" style= "margin-top:0px; margin-left:0px; padding-left:0px; padding-top:0px; ">
<div style= "margin-top:0px; margin-left:0px; padding-left:0px; padding-top:0px; ">
<img alt="" src="" id ="ImagePrv" runat="server"
style= "margin-top:0px; margin-left:0px; padding-left:0px; padding-top:0px; height: 120px; width: 110px;"/> </div>
<br /><br /><br />
<div style= "visibility:visible">
<cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" ThrobberID="tid1"
onclientuploadcomplete="OnClientAsyncFileUploadComplete" />
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
我有一个名为ImageRequestHandler.ashx的httphandler ImageRequestHandler.ashx的代码隐藏
我希望如此,如果有任何javascript解决方案,我已经尝试document.geteleme。。。。但是它不起作用!!
提前感谢即使您的iframe已嵌入到Frm_MngAddGoodsGrp.aspx中,您也需要将Frm_MngAddGoodsGrp.aspx和OpenDialog.aspx视为单独的页面(它们是独立的)。您不能直接从一个页面的代码更改另一个页面的代码 也就是说,有两种方式可以使承载iframe的页面与承载在iframe中的页面进行通信:
src
属性,因此可以将查询字符串中的值传递给子页面李>
假设您的父页面知道新图像的id是什么,我会将iFrame的src属性设置为
OpenDialog.aspx?imageId=12345
,并让OpenDialog.aspx从其查询字符串中检索值,并使用它设置图像控件的ImageUrl
属性 即使您的iframe嵌入到Frm_MngAddGoodsGrp.aspx中,您也需要将Frm_MngAddGoodsGrp.aspx和OpenDialog.aspx视为单独的页面(它们是独立的)。您不能直接从一个页面的代码更改另一个页面的代码
也就是说,有两种方式可以使承载iframe的页面与承载在iframe中的页面进行通信:
src
属性,因此可以将查询字符串中的值传递给子页面李>
假设您的父页面知道新图像的id是什么,我会将iFrame的src属性设置为
OpenDialog.aspx?imageId=12345
,并让OpenDialog.aspx从其查询字符串中检索值,并使用它设置图像控件的ImageUrl
属性 如果你能把你的描述分成不同的段落,这会更清楚。只有我的两分钱,重新加载iFrame不是很有效吗?您的iFrame页面将重新加载,并且可以在会话中访问信息。您介意告诉我更多关于它的信息吗。我想访问iframe中的图像我知道如何显示图像,但我无法访问side iframe中的图像我只想将src属性设置为我的httphandler我不知道如何更清楚地描述它…您的第二个代码示例:OpenDialog.aspx背后的代码,或者对于Frm_MngAddGoodsGrp.aspx?它是Frm_MngAddGoodsGrp.aspx的代码。我编辑问题以声明更多。如果您可以将您的描述分成单独的段落,这将更清楚。只有我的两分钱,重新加载iFrame不是很有效吗?您的iFrame页面将重新加载,并且可以在会话中访问信息。您介意告诉我更多关于它的信息吗。我想访问iframe中的图像我知道如何显示图像,但我无法访问side iframe中的图像我只想将src属性设置为我的httphandler我不知道如何更清楚地描述它…您的第二个代码示例:OpenDialog.aspx背后的代码,或者对于Frm_MngAddGoodsGrp.aspx?它是Frm_MngAddGoodsGrp.aspx的代码我编辑问题以声明它更多此描述告诉我如何传递某些东西(参数)对于嵌入在iframe中的页面,但我使用的算法是使用sessioni,我已经用我的表单中的一个简单图像做了我想要做的事情,但是我想对iframe中的图像做这件事,它的src设置为OpenDialog,我的父页面知道图像的字节数组并将其发送到会话,但页面中有一个按钮触发AsyncFileUpload的浏览,但是
public partial class OpenDialog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static readonly string STORED_IMAGE = "SessionImage";
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.PostedFile != null)
{
HttpPostedFile file = AsyncFileUpload1.PostedFile;
byte[] data = ReadFile(file);
Session[STORED_IMAGE] = data;
}
}
private byte[] ReadFile(HttpPostedFile file)
{
byte[] data = new Byte[file.ContentLength];
file.InputStream.Read(data, 0, file.ContentLength);
return data;
}
}
public class ImageRequestHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if (context.Request.QueryString.Count != 0)
//if (context.Request.Cookies.Count != 0)
{
var storedImage = context.Session[OpenDialog.STORED_IMAGE] as byte[];
if (storedImage != null)
{
Image image = GetImage(storedImage);
if (image != null)
{
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
private Image GetImage(byte[] storedImage)
{
var stream = new MemoryStream(storedImage);
return Image.FromStream(stream);
}
public bool IsReusable
{
get { return false; }
}
}