javascript
  • c#
  • webforms
  • 2016-06-08 5 views 1 likes 
    1

    웹 사이트로 처음 들어 왔을 때만 사용자에게 보내려고한다는 경고가 나타납니다. 조건은 C#이고 그것은 내가 사용한 코드입니다. 알렛을 보내지 않습니다. 어떻게해야합니까? 당신은 ASP.NET 웹 양식을 사용하는 경우C# 백엔드에서 브라우저로 스크립트를 보냅니다.

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

    답변

    1

    u는이 같은 ScriptManager를 사용할 수 있습니다

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

    당신이 내용이 스크립트 태그에 싸여 얻을 것이다 사실에 마지막 매개 변수를 설정합니다.

    코드는 다음과 같습니다

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

    이 코드를 사용해보십시오.

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

    이 고려 될 ASPX

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

    ASPX.CS 코드

    은, 당신의 문자 항목은 최종 바디 HTML 태그에 추가해야합니다. 도움이 되었기를 바랍니다.

    관련 문제