天天看点

Asp.net访问Iframe

  最近在做一个邮件系统!运用了toolbar+iframe布局!其中toolbar有这样两个按钮:删除邮件和清空邮件夹!同时,在邮件夹里面也有这样两个按钮!其实.这里涉及到的问题就是JS访问iframe内button控件,并执行button的click事件的问题!由于我运用的大部分是服务器控件,而且,toolbar一般比iframe早加载.所以出现JS访问不到控件的结果!起初我想动态加载toolbar控件,但考虑到上面的按钮比较复杂,所以我就另外想了个办法,在页面末尾加了个两钮:delbt1跟clearbt1,我们只能执行这两个按钮的事件就能访问到iframe里面的内容了!!

  废话少说!把代码共享给大家吧!!希望大家有用:

前台页面:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="mailindex.aspx.vb" Inherits="mailindex" %>

<%@ Register Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="Microsoft.Web.UI.WebControls" TagPrefix="iewc" %>

<!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 href ="body.css" rel ="Stylesheet" type ="text/css" />

          <script language ="javascript" type ="text/javascript" >

        //触发iframe中删除按钮的事件

       function get_delbt()

       {

       var info=window.confirm('確定要刪除該郵件嗎?');

         if (info==true)

       {

//这句很关键,通过找到mainframe,运用dom访问其中的控件btdel(删除按钮),执行服务器操作click

          document.getElementById("mainframe").contentWindow.document.getElementById("btdel").click();

       }

       }  

       //触发iframe中清空按钮的事件

       function get_delallbt()

       {

       var info=window.confirm('確定要清空該郵件夾嗎?');

       if (info==true)

       {

          document.getElementById("mainframe").contentWindow.document.getElementById("btalldel").click();

       }

       }   

    </script>

</head>

<body>

    <form id="form1" runat="server">

            <!--#include virtual="head.inc"-->

  <div align ="center" >

        <table width ="750px" background="images/back.gif">     

        <thead >

        <tr>

        <th colspan ="5" align ="left" >

            <iewc:Toolbar ID="Toolbar1" Width ="400px" DefaultStyle ="background-image:url(images/BackgroundT.bmp)" ToolTip ="工具欄"  OnButtonClick ="toolbarbt_click" runat="server">

            <iewc:ToolbarButton ID="inputbt" ImageUrl="TreeLineImages/inbox.bmp"  Text ="接收郵件"/>

            <iewc:ToolbarButton ID="newbt" ImageUrl="TreeLineImages/unreader.bmp" Text ="撰寫郵件" />

            <iewc:ToolbarSeparator />

            <iewc:ToolbarButton ID="delbt" ImageUrl="TreeLineImages/unout.bmp" Text= "刪除郵件" />

            <iewc:ToolbarSeparator />

            <iewc:ToolbarButton ID="clearbt" ImageUrl="TreeLineImages/reback.bmp"  Text ="清空郵件" />

            <iewc:ToolbarSeparator />

            <iewc:ToolbarButton ID="infobt" ImageUrl="TreeLineImages/hold.bmp" Text= "信息統計" />

            <iewc:ToolbarSeparator />

            <iewc:ToolbarButton ID="addressbt" ImageUrl="TreeLineImages/mailbox.bmp" Text ="地址薄" />        

            </iewc:Toolbar>

           </th>

        </tr>

        </thead>

        <tbody >

            <tr>

                <td colspan="4" valign ="top"  rowspan="2">

                     <iframe id="mainframe" name ="mainframe" runat ="server" scrolling="auto" width ="600px" height ="400px" ></iframe>

                </td>            

            </tr>

            <tr>

            </tr>        

            </tbody>

            <tfoot >

            <tr>

            </tr>

            </tfoot>

        </table>

        </div>

        <input id="delbt1" type="button" runat ="server"  value="delete" />

        <input id="clearbt1" type="button"  runat ="server"  value="clear" />     

    </form>

</body>

</html>

    Sub toolbarbt_click(ByVal sender As Object, ByVal e As EventArgs)

        Select Case sender.id.ToString

            Case "inputbt"

                inputmail()

                Return

            Case "newbt"

                Response.Write("<script>javascript:window.showModelessDialog('sendmail.aspx',window,'dialogWidth:800px;resizable:yes;');</script>")

                Return

            Case "delbt"

                Me.delbt1_ServerClick(delbt1, New EventArgs)       '触发delbt1的click事件

            Case "clearbt"     

                Me.clearbt1_ServerClick(clearbt1, New EventArgs)    '触发clearbt1的click事件,下面红色部分

            Case "infobt"

                Response.Write("<script>javascript:window.showModelessDialog('mailinfo.aspx',window,'dialogWidth:800px;resizable:yes;');</script>")

                Return

            Case "addressbt"

                Response.Write("<script>javascript:window.showModelessDialog('mailbook.aspx',window,'dialogWidth:800px;resizable:yes;');</script>")

                Return

            Case Else

                Response.Write("請選擇按鈕")

        End Select

    End Sub

    Protected Sub delbt1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles delbt1.ServerClick

'调用JS方法:get_delbt().执行iframe中删除按钮的事件!

        Page.ClientScript.RegisterStartupScript(GetType(String), "", "<script>javascript:get_delbt();</script")

    End Sub

    Protected Sub clearbt1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearbt1.ServerClick

'调用JS方法:get_delallbt().执行iframe中清空按钮的事件!

        Page.ClientScript.RegisterStartupScript(GetType(String), "", "<script>javascript:get_delallbt();</script")

    End Sub