DIV CSS 佈局教程網

ASP.NET 中用 showModalDialog 傳值回主页面的完整流程
編輯:關於JavaScript     

一、Default.aspx:主頁面

<script type="text/javascript">
function GetUser(TxtEmpID, TxtEmpName, url) {
    var properties = 'status:no;resizable:no;toolbar=no;menubar=no;' +
                     'location=no;scroll:no;dialogWidth:540px;dialogHeight:450px;';
    var arg = window.showModalDialog(url, "", properties);
    if (arg != null) {
        document.getElementById(TxtEmpID).value   = arg[0];
        document.getElementById(TxtEmpName).value = arg[1];
    }
}
</script>

<table width="240px" cellpadding="0" cellspacing="0">
<tr>
  <td style="width: 80px">
    <asp:Label ID="Label1" runat="server" Text="選擇課級別一" Width="80px" class="font-w"></asp:Label>
  </td>
  <td style="width: 80px">
    <asp:TextBox ID="TextBox1" runat="server" ReadOnly="false" Width="80px" onfocus="this.blur()"></asp:TextBox>
  </td>
  <td style="width: 80px">
    <asp:TextBox ID="TextBox2" runat="server" ReadOnly="false" Width="80px"></asp:TextBox>
  </td>
  <td style="width: 80px">
    <asp:Button ID="Button1" runat="server" Text="確定" OnClick="Button1_Click" CssClass="ButtonCss"/>
  </td>
  <td><asp:Label ID="Label3" runat="server" Text=""></asp:Label></td>
  <td><asp:Label ID="Label4" runat="server" Text=""></asp:Label></td>
</tr>
</table>

二、Default.aspx.cs:後端綁定 ClientID

protected void Page_Load(object sender, EventArgs e) {
    TextBox1.Attributes.Add("onclick",
        "GetUser('" + this.TextBox1.ClientID + "','" +
        this.TextBox2.ClientID + "','WebDialog.aspx)");
}

這一步是 ASP.NET 的關鍵:伺服器控制項的 ID 與最終輸出的 client id 可能不同(尤其被包在命名容器中時),所以必須用 ClientID 把真正的 id 傳給前端函式。

三、WebDialog.aspx:彈出視窗

<script language="javascript" type="text/javascript">
function Submit() {
    var arr = new Array();
    arr[0] = document.getElementById('TxtEmpID').value;
    arr[1] = document.getElementById('TxtEmpCName').value;
    arr[2] = document.getElementById('TxtTitleID').value;

    window.returnValue = arr;   // 把陣列設為回傳值
    window.close();
}
</script>

<table>
<tr><td>
  <asp:TextBox ID="TxtEmpID"    runat="server" Width="50px" ReadOnly="true" CssClass="InputCss"></asp:TextBox>
  <asp:TextBox ID="TxtEmpCName" runat="server" Width="60px" ReadOnly="true" CssClass="InputCss"></asp:TextBox>
  <asp:TextBox ID="TxtTitleID"  runat="server" Width="40px" ReadOnly="true" CssClass="InputCss"></asp:TextBox>
</td></tr>
<tr><td>
  <input id="Button1" type="button" value="確 定" onclick="Submit()"/>
</td></tr>
</table>

流程串起來

  1. 使用者點擊 TextBox1,觸發 GetUser,並传入兩個 TextBox 的 ClientID 與彈出頁網址。
  2. showModalDialog 以模式開啟 WebDialog.aspx,主頁面會暫停等待。
  3. 使用者在彈出視窗中選擇資料,按確定後把結果陣列指定給 window.returnValue,再關閉視窗。
  4. 控制權回到主頁面,arg 拿到那個陣列,依序填入兩個 TextBox。

實務提醒

showModalDialog 只支援舊版 IE。它從未成為標準,Chrome 與 Firefox 都不實作(Chrome 曾短暫支援後移除)。跨瀏覽器專案請改用 window.open 搭配 window.opener 回寫,或自己做一個 modal 覆蓋層搭配 AJAX。

注意 TextBox1 上的 onfocus="this.blur()"。這是為了讓唯讀欄位不要取得焦點,屬於體驗上的小細節。

Dialog 中的 TextBox 同樣要用 ClientID。範例直接寫 getElementById('TxtEmpID') 是因為該頁沒有命名容器;若控制項被包在 Master Page 或 UpdatePanel 裡,一樣得改用 ClientID。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved