2013-12-13 21 views
6

Başka bir sayfaya yönlendirildikten sonra JavaScript'i işin arkasında koduma nasıl getirebilirim? Bir asp buton kontrolüm var ve o düğmeye tıkladığımda uyarmak istiyorum, sonra başka bir sayfaya gidin. Kodumda (JS kodundan önce veya sonra) bir Response.Redirect bulunduğumda, 8 denemenin hiçbiri çalışmaz. Yönlendirme yaptığımda, birkaç (2,7 & 8) çalışır.Kod Yönlendirme ile JavaScript uyarısı

//Try one 
ScriptManager.RegisterStartupScript(this, GetType(), "test", "alert('test1');", true); 

//Try two 
ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "test2"); 

//Try three 
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "alertMessage()", true); 

//Try four 
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "alertMessage()", true); 

//Try five 
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "javascript: alertMessage(); ", true); 

//Try six 
ClientScript.RegisterClientScriptBlock(GetType(), "CallMyFunction", "<script>alert('test4')</script>"); 

//Try seven 
Response.Write("<script>alert('test5');</script>"); 

//Try eight 
string script = "alert('test6')"; 
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CallMyString", script, true); 

response.redirect("pageUrlHere"); 
//With this code above, none of the js functions (alerts) work 

//response.redirect("pageUrlHere"); 
//With this commented out, try 2, 7 and 8 work. 

JS işlevi:

function alertMessage() { 
    alert('test3'); 
} 

cevap

7

Aşağıdaki deneyebilirsiniz:

ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect", 
"alert('test 9'); window.location='" + 
Request.ApplicationPath + "/anotherpage.aspx';",true); 
+0

Teşekkürler arkadaşı çalışır! –

+0

Endişeye gerek yok, bu yardımcı oldu :) – chridam

+1

Küçük bir değişiklik, sayfa adı ScriptManager.RegisterStartupScript (bu, this.GetType(), "yönlendirme", "uyarı ('test 9') önce pencere gerekir. location = '"+ Request.ApplicationPath +" /anotherpage.aspx'; ", true);' – n00b

4

o uyarı görüntüler bu deneyin ve sadece yeniden yeniden ayrı yöntemle bunu yapmak gidin.

public void ShowAlertAndNavigate(string msg , string destination) 
    { 
     string alert_redirect_Script = string.Format(@"<script type=""text/javascript""> 
             alert('{0}'); 
             window.location.href = destination; 
             </script>", msg); 
     ClientScript.RegisterClientScriptBlock(this.GetType(), "alertredirectscript", alert_redirect_Script, false); 
    } 
İlgili konular