最近在做一個郵件系統!運用了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