.Net調用Javascript代碼開啟小表單

來源:互聯網
上載者:User

最近做的系統中需要使用者在介面上彈出一個小視窗,就像下面這樣

顯然用.net是實現不了的,需要用js,據說js就是產生各種炫酷效果的。於是仿照寫了點代碼,也參考了前人的代碼:

1、在主表單中加入

<script language="javascript" type="text/javascript">function openDialog(url,width,height){var obj = new Object();    obj.name = '';    obj.code = '';var ret = window.showModalDialog(url,obj,"dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:0;resizable:0;center:1;help:0;scroll:yes;");if(ret == true){    event.returnValue = false;    return obj;}else{    return null;} return obj;}</script>

2、在主表單中button_click事件中添加

int flag = Convert.ToInt32(gdvcharacter.SelectedRow.Cells[0].Text); string temp = "Opcharacter.aspx?Page=3&ID="+flag; ScriptManager.RegisterStartupScript(Page, this.GetType(), "myscript", "openDialog('" + temp + "',650,240);", true);

上面有幾個參數,其中最關鍵的就是最後一句話,是調用前台的js

然後就能看到效果了,不過在關閉彈出的對話方塊的時候發現問題,老是無法關閉,反而是開啟一個新表單,查了半天資料也不知道如何解決。更讓我詫異的是,在chrome下能關閉,在IE下就不能關閉,不知道Firefox又如何,看來瀏覽器安全色性問題。

細細分析後,發現chrome把彈出頁是當成一個新表單開啟的,而IE是當成一個對話方塊開啟的,按照常理來說應該是IE反應正確,chrome出現邏輯錯誤了。

請假公司的一個懂點JS的牛人,忙了半個小時,才把問題搞定,其實本質我也不太清楚,但實現就行了。

解決方案:

1、在彈出的對話方塊頁面中添加一個iframe

 <iframe id="test_f" style="display:none" name = "test_f">     </iframe>

2、設定原來的from

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

3、添加一個js函數

    <script language=javascript>    function myTest(){    window.close();    }    </script>

4、在彈出頁中的關閉事件中修改下

原來是:

Response.Write("<script>windows.close();</script>");

修改成:

Response.Write("<script>top.myTest();</script>");

然後就能看到能正常關閉了。

iframe很神奇啊,上網查了一下,是一種自訂的架構,但和這個彈出有什麼關係呢?還是有點不懂,要學習的東西太多了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.