2011-02-16 102 views
4

我有一個asp.net頁面,其中包含嵌入了一些數據和ImageButton的Iframe。上的ImageButton單擊事件(服務器端)我有Response.Redirct:如何打開Iframe中的重定向頁面在ASP.NET的父窗口中打開?

Response.Redirect("results.aspx"); 

這總是在iframe中打開results.aspx。我希望results.aspx應該始終在父窗口中打開。我嘗試了以下直到現在,但沒有工作:

Response.Redirect("<script language='javascript'>self.parent.location='results.aspx';</script>"); 
Response.Redirect("javascript:parent.change_parent_url('results.aspx');"); 

作爲迴應Rifk,我添加ClientScriptManager。 的.aspx有這個條目:

<asp:ImageButton ID="ImageButton_ok" ImageUrl="~/images/ok.gif" 
     OnClick="btnVerify_Click" OnClientClick="ValidateFields()" 
     runat="server" /> 

代碼背後的Page_Load():

ClientScriptManager cs = Page.ClientScript; 
StringBuilder myscript = new StringBuilder(); 
myscript.Append("<script type=\"text/javascript\"> function ValidateFields() {"); 
myscript.Append("self.parent.location='default.aspx';} </"); 
myscript.Append("script>"); 
cs.RegisterClientScriptBlock(this.GetType(), "ButtonClickScript", myscript.ToString()); 

btnVerify_Click有主要業務邏輯。如果我的業務邏輯失敗,我將如何停止OnClientClick()來觸發?或者,當服務器端代碼成功執行時,我該如何觸發?

回答

8

Response.Redirect只會影響iFrame中的頁面,如果這是在服務器端執行重定向的頁面。您想要在該iFrame中運行一些將重定向父級的JavaScript,就像您在第二個示例中所述。爲了運行該腳本,您不應該使用Response.Redirect(),而應該註冊客戶端腳本。

請參閱以下鏈接爲如何在你的代碼在ASP.Net 2.0註冊的客戶端腳本 - Using Javascript with ASP.Net 2.0

例如,你會在你的事件處理該ImageButton的末尾添加類似於此點擊:

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myUniqueKey", 
        "self.parent.location='results.aspx';", true); 
+0

我很抱歉,但多少有些失落。不知道如何實現它。在我的IFrame頁面上寫什麼?你想讓我使用Page.ClientScript.RegisterClientScriptBlock()方法嗎?但是如何通過我的ImageButton click事件來連接它? – 2011-02-17 13:45:48

+0

在您的圖像按鈕上,您可以設置OnClientClick屬性以運行javascript方法,如下所述:http://www.w3schools.com/aspnet/prop_webcontrol_imagebutton_onclientclick.asp。然後你的javascript方法就會有你上面提供的語句,比如:「self.parent.location ='results.aspx';」。 – 2011-02-17 16:23:40

+0

Rifk,這樣做會使OnClientClick(客戶端代碼)無需等待我對OnClick(服務器端代碼)的處理即可運行。對?我們在OnClick中有一些業務邏輯需要在重定向之前執行。感謝您的時間和解釋。 – 2011-02-17 20:54:30

1

感謝Rifk的解決方案。這裏是有類似問題的人的代碼:

在aspx文件中,我定義了一個新的JS函數Redirection()。 ValidateFields()函數將執行一些客戶端驗證。

<head runat="server"> 
    <title>Untitled Page</title> 
    <script language="javascript" type="text/javascript"> 
    function ValidateFields() 
    { 
     alert ("Some client side validations!!"); 
    } 

    function Redirection() 
    { 
     self.parent.location="http://www.google.com"; 
    } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <h2>Content - In IFrame</h2> 
     <asp:CheckBox ID="chkValid" runat="server" /> 
     <asp:ImageButton ID="ImageButton_FillW8Online" ImageUrl="~/images/expand.gif" 
     OnClick="btnVerify_Click" OnClientClick="return ValidateFields()" 
      runat="server" style="height: 11px" /> 

    </div> 
    </form> 
</body> 

在代碼後面,我有非常簡單的代碼,在做一些服務器端驗證後註冊clientscriptblock。我要求重定向只有在服務器端驗證成功的情況下才會發生。

bool isValid = false; 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void btnVerify_Click(object sender, EventArgs e) 
{    
    //do some validations 
    isValid = chkValid.Checked; 
    if (isValid) 
     this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "Redirection()", true); 
} 
2

我有一個asp.net頁面,其中包含嵌入了一些數據和按鈕的iframe。在按鈕點擊事件(服務器端)我有Response.Redirct,但我需要關閉Iframe並加載父頁面。添加下面提到的腳本解決了問題。

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myUniqueKey",      "self.parent.location='results.aspx';", true); 
1

你可以試試這個:

Response.Write("<script>window.open('page.aspx','_parent');</script>"); 

問候。

0
Response.Clear(); 
Header.Controls.Add(new LiteralControl(@" 
<script type=""text/javascript""> 
top.location = ""/Logout.aspx""; 
parent.location = ""/Logout.aspx""; 
</script> 
")); 
0

如果你只是想直接開一個網站「過」當前頁面與IFRAME(不是新的標籤或窗口),那麼你並不需要的代碼隱藏。

即:

<asp:LinkButton ID="lnkGeneralEnq" runat="server" OnClientClick="OpenOverFrame();"><strong>click this link</strong></asp:LinkButton> 

和一行中的Java腳本代碼在你的ASPX頁面有點...

function OpenOverFrame() { 
      window.open('http://mywebsite.com','_parent'); 
     } 
相關問題