2013-05-27 32 views
0

我正面臨着返回父頁面的問題。 請幫助。如何從window.ShowModelDialog中將值返回給父頁面?

我有一個gridview允許用戶在該行添加新記錄,但用戶也可以點擊該行上的搜索按鈕,我將彈出下面的代碼。

 TextBox txtCarNo = gvLookup.FooterRow.FindControl("txtNo") as TextBox;   
     System.Text.StringBuilder s = new System.Text.StringBuilder(); 
     s.Append("<script language='javascript' id='SearchResult'> "); 
     s.Append("var WinSettings = 'dialogHeight:400px ; dialogWidth: 550px ;center: Yes ;resizable: No;status: no'; "); 
     s.Append("javascript: window.showModalDialog('Search.aspx?no=" + txtNo.Text.Trim().ToUpper() + "','',WinSettings); "); 
     s.Append("</script > ");   

     if ((!ClientScript.IsStartupScriptRegistered("SearchResult"))) 
     { 
      ClientScript.RegisterStartupScript(this.GetType(), "SearchResult", s.ToString()); 
     } 

在子頁面,就會有另一個GridView控件,顯示搜索結果,用戶可以在該行的一個點擊返回數字回父頁面。 我想過使用Session來傳遞值,但是當顯示ShowModalDialog時,父頁面上的代碼已經通過,這意味着Session不能在這種情況下工作。

請指教我如何返回一個值到父頁面。 非常感謝。

回答

1

Example from Wrox

當你打電話的showModalDialog你這樣做:

var oReturnValue = showModalDialog(....); 

在在showModalDialog,假設你的文本框有 「txtForename」 和 「txtSurname」 的標識:

<body onbeforeunload="terminate();"> 
function terminate() 
{ 
    var o = new Object(); 
    o.forename = document.getElementById("txtForename").value; 
    o.surname = document.getElementById("txtSurname").value; 
    window.returnValue = o; 
} 

然後繼續在您的主窗口中:

alert(oReturnValue.forename + "\n" + oReturnValue.surname); 
+0

嗨丹尼,非常感謝您的幫助。我應該在哪裏把警報放在主窗口中? – EngLoon

+0

如果您需要警報,則應該在showModalDialog()命令之後執行。 – Ted

+0

嗨丹尼。實際上,在用戶從ShowModalDialog中選擇一個數字後,我需要在主頁面的gridview中將選定的數字顯示到頁腳行中。我面臨的問題是我不知道如何將選定的編號分配到頁腳行中。 :) – EngLoon

相關問題