2016-06-08 70 views
1

我有一個提醒,我想只發送用戶在他的第一個條目到網站。條件是在C#中,這就是我使用的代碼。它不發送alret。我該怎麼辦?從C#後端發送腳本到瀏覽器

if ((string)Session["alert"] == null) { 
     Response.Write("<script type='text/javascript'>alert('WELCOME to the website!\nEnjoy!')</script>"); 
     Session["alert"] = "done"; 
    } 

回答

1

如果您正在使用ASP.NET Web窗體u可以使用ScriptManager這樣的:

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('WELCOME to the website!\nEnjoy!')", true); 

如果您設置的最後一個參數設置爲true的內容將得到包裹在腳本標記。

您的代碼應該是這樣的:

if ((string)Session["alert"] == null) { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('WELCOME to the website!\nEnjoy!')", true); 
     Session["alert"] = "done"; 
} 
0

試試這個代碼。

ASPX

<body> 
<form id="form1" runat="server"> 
<asp:Literal id="ltrMessage" runat="Server"/> 
</form> 
</body> 

ASPX.CS代碼

if ((string)Session["alert"] == null) { 
    ltrMessage.Text="<script type='text/javascript'>alert('WELCOME to the website!\nEnjoy!')</script>"; 
    Session["alert"] = "done"; 
} 

來算,你的文字資料應加入到端部主體html標記。 我希望有所幫助。