asp.net เรียกจาวาสคริปต์จาก Code Behind

ฉันใช้ NOTIFY ในอินพุต html เพื่อแจ้งผู้ใช้ของฉันว่าบันทึกได้รับการบันทึกแล้วหรือไม่ และมันก็ใช้ได้ดี แต่เมื่อฉันพยายามใช้มันจากโค้ดที่อยู่หลังไฟล์ มันก็กลับไม่ใช่ ฉันเข้าใจว่าจาวาสคริปต์เป็นเทคโนโลยีฝั่งไคลเอ็นต์ และได้ลองใช้ RegisterStartupScript แต่ก็ไม่ประสบผลสำเร็จ

ฉันกำลังลองใช้มันด้วยการคลิกปุ่มแบบนี้

        protected void Button1_Click1(object sender, EventArgs e)
    {
        var script = " $.notify.success('I do not want to close by myself close me ', { close: true });";
        ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", script, true);

    }

แต่ไม่มีโชค

ฉันแน่ใจว่าจะต้องมีวิธีแสดงแถบการแจ้งเตือนที่ด้านบนเมื่อฐานข้อมูลได้รับการอัปเดตแล้ว สมมติว่าเราสามารถทำได้โดยใช้ฟังก์ชันหรือไม่
สคริปต์ของฉันถูกกำหนดดังนี้

 <!-- Notify Implementation -->
<script src="../Scripts/jquery-1.9.0.js" type="text/javascript"></script>
<link href="/th../Styles/notify.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/notify.js" type="text/javascript" ></script>

<script type="text/javascript">

     function myNotify() {
         $.notify.success('I do not want to close by myself close me ', { close: true });
     };

Can somebody help please


person Shezi    schedule 28.01.2013    source แหล่งที่มา
comment
โปรดดูคอนโซลของเบราว์เซอร์เพื่อดูว่ามีข้อผิดพลาดหรือไม่   -  person phnkha    schedule 28.01.2013
comment
ลองดูแหล่งที่มาของหน้าจากเบราว์เซอร์ของคุณและดูว่าสคริปต์แสดงผลอยู่ในนั้นหรือไม่   -  person Iswanto San    schedule 28.01.2013
comment
@ namkha87 ไม่มีข้อผิดพลาดเกิดขึ้น   -  person Shezi    schedule 28.01.2013
comment
@IswantoSan ฉันคิดว่ามันถูกเรนเดอร์นี่คือสิ่งที่ฉันพบในแหล่งที่มาของหน้า ‹input type=submit name=ctl00$MainContent$Button1 value=Button onclick=myNotify();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(ctl00$MainContent$Button1, , true , , , เท็จ, เท็จ)) id=MainContent_Button1 /›   -  person Shezi    schedule 28.01.2013


คำตอบ (3)


ลองแบบนี้:-

  System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script language='javascript'>");
                sb.Append("function notify(){");
                sb.Append("$.notify.success('I do not want to close by myself close me ', { close: true });");
    sb.Append("}");
                sb.Append("/script>");

 ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", sb, true);
person Pranav    schedule 28.01.2013
comment
คอนโซลของเบราว์เซอร์ส่งข้อยกเว้น Uncaught SyntaxError: โทเค็นที่ไม่คาดคิด ‹ - person Shezi; 28.01.2013
comment
นั่นคือสิ่งที่คอนโซล Chrome แสดงเท่านั้น .. Uncaught SyntaxError: โทเค็นที่ไม่คาดคิด ‹ - person Shezi; 28.01.2013
comment
และข้อผิดพลาดนี้เกิดขึ้นในบรรทัด ‹script type=text/javascript›function notify(){var script = $.notify.success('I do not want to close by self close me ', { close: true });} ‹/สคริปต์›//]]› - person Shezi; 28.01.2013
comment
พี่ชาย... อีเมลของฉันคือ aahar ที่ gmail ดอทคอม... ส่งที่อยู่อีเมลของคุณมาให้ฉัน ฉันจะให้เซสชันระยะไกลแก่คุณเพื่อให้คุณสามารถตรวจสอบได้ - person Shezi; 28.01.2013
comment
โปรดใช้การแชร์ระยะไกลและดูว่าปัญหาอยู่ที่ไหน หากคุณมีเวลาเท่านั้น - person Shezi; 28.01.2013

ลองสิ่งนี้:

Page.ClientScript.RegisterStartupScript(this.GetType(),"ButtonAlert","myNotify()",true);
person Srinivas    schedule 28.01.2013
comment
คอนโซลของเบราว์เซอร์ส่งข้อยกเว้น Uncaught TypeError: ไม่สามารถเรียกเมธอด 'attr' ของไม่ได้กำหนด - person Shezi; 28.01.2013

วิธีนี้แก้ไขปัญหาของฉันตามที่อธิบายไว้ใน ที่นี่ . ฉันใช้คลาสตัวช่วยและมันช่วยแก้ไขปัญหาได้

    using System.Web.UI;

public static class NotificationHelper
{
    /// <summary>
    /// Shows the successful notification.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="message">The message.</param>
    public static void ShowSuccessfulNotification(this Page page, string message)
    {
        page.ClientScript.RegisterStartupScript(page.GetType(), "notificationScript",
                                                "<script type='text/javascript'>  $(document).ready(function () {  $.notify.success('I do not want to close by myself close me ', { close: true });});</script>");
    }
}
person Shezi    schedule 28.01.2013
comment
ใช่... ขอบคุณสำหรับความช่วยเหลือและเวลาครับพี่ชาย ฉันไม่สามารถทำเครื่องหมายคำตอบของคุณว่าแก้ไขแล้ว แต่จะลงคะแนนอย่างแน่นอน ขอบคุณอีกครั้งครับพี่ชาย - person Shezi; 28.01.2013