簡體   English   中英

從c#調用javascript函數不起作用

[英]call javascript function from c# not working

大家好,我在popupscrit.js文件中有這個方法:

function afficherMessageInfo( id, message) {


    //Get the A tag
    alert('executing the client code');

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect     
    $('#mask').fadeIn(500); 
    $('#mask').fadeTo("slow",0.8);  

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //transition effect
    $(id).fadeIn(1000);




}

$('.window .close').live("click", function (event) {
//Cancel the link behavior
event.preventDefault();
$('#mask').hide();
$('.window').hide();
});

//if mask is clicked
$('#mask').live("click", function (event) {
  $(this).hide();
  $('.window').hide();
});     

我想顯示一個來自ac#button click事件的彈出窗口,如下所示:

void BtnEnregistrer_btnClick(object sender, EventArgs e)
    {
        string id = "#info";
        string messageInfo = "le dossier a été crée avec succès";


        string script = String.Format("afficherMessageInfo({0},{1})", id, messageInfo);
        this.Page.ClientScript.RegisterStartupScript(this.GetType(),
        "afficherMessageInfo", script, true);

    }

和HTML部分:

<script src="../Scripts/jquery.js" type="text/javascript"></script>
<script src="popupScript.js" type="text/javascript"></script>

</head>

<body>
 <form id="form1" runat="server">
<div id="boxes">
<!-- information -->
    <div id="info" class="window" style="display:none;">
        <div class="info_title">
            <div class="pop_title">Information</div>
            <div class="close_image"><a href="#"class="close"/><img src="images/popups/close.jpg" border="0" /></a></div>
        </div>
        <div class="pop_content">
            <div class="pop_message">Etiam vel nisl ante. Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante.</div>
            <div align="right"><input name="" type="button" value="OK" class="info_bt"/></div>
        </div>
    </div>
<!-- Alerte -->
    <div id="alerte" class="window" style="display:none;">
        <div class="alerte_title">
            <div class="pop_title">Alerte</div>
            <div class="close_image"><a href="#"class="close"/><img src="images/popups/close.jpg" border="0" /></a></div>
        </div>
        <div class="pop_content">
            <div class="pop_message">Etiam vel nisl ante. Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante.</div>
            <div align="right"><input name="" type="button" value="OK" class="alerte_bt"/></div>
        </div>
    </div>
<!-- validation -->
    <div id="validation" class="window" style="display:none;">
        <div class="validation_title">
            <div class="pop_title">Validation</div>
            <div class="close_image"><a href="#"class="close"/><img src="images/popups/close.jpg" border="0" /></a></div>
        </div>
        <div class="pop_content">
            <div class="pop_message">Etiam vel nisl ante. Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante.</div>
            <div align="right"><input name="" type="button" value="Annuler" class="validation_bt"/> <input name="" type="button" value="OK" class="validation_bt"/></div>
        </div>
    </div>
<!-- confirmation -->
    <div id="confirmation" class="window" style="display:none;">
        <div class="confirmation_title">
            <div class="pop_title">Demande de Confirmation</div>
            <div class="close_image"><a href="#"class="close"/><img src="images/popups/close.jpg" border="0" /></a></div>
        </div>
        <div class="pop_content">
            <div class="pop_message">Etiam vel nisl ante. Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante.</div>
            <div align="right"><input name="" type="button" value="Exemple 1" class="confirmation_bt"/> <input name="" type="button" value="Exemple 2" class="confirmation_bt"/> <input name="" type="button" value="Exemple 3" class="confirmation_bt"/> <input name="" type="button" value="Annuler" class="confirmation_bt"/> <input name="" type="button" value="OK" class="confirmation_bt"/></div>
        </div>
    </div>
<!-- Masque pour couvrir la page -->
    <div id="mask"></div>
</div>

什么是警報沒有顯示的pb

看起來您傳遞給javascript函數的參數缺少引號:

這條線

string script = String.Format("afficherMessageInfo({0},{1})", id, messageInfo); 

應該讀

string script = String.Format("afficherMessageInfo('{0}','{1}')", id, messageInfo); 

在afficherMessageInfo()后你缺少一個分號...

你試過使用瀏覽器工具調試嗎?

刪除RegisterStartupScript函數調用中的true參數,原因是它已經在標記中已經在<script></script>塊中確定。

RegisterStartupScript有一個重載,最后不include script block param

所以改變你的行:

this.Page.ClientScript.RegisterStartupScript(this.GetType(),
"afficherMessageInfo", script);

為什么不添加OnClientClick屬性?

protected void Page_Load(object sender, EventArgs e)
{
    string id = "#info";
    string messageInfo = "le dossier a été crée avec succès";

    BtnEnregistrer.OnClientClick = String.Format("afficherMessageInfo('{0}','{1}')", id, messageInfo);

}

這個方法會限制你使用PostBack並立即給你一個彈出窗口。

// Round your values before using them since they can result in float.
// Also you have to implicitly add '.px' to them.

//Get the screen height and width
var maskHeight = $(document).height() + '.px';
var maskWidth = $(window).width() + '.px';

//Set heigth and width to mask to fill up the whole screen
$('#mask').css({
    'width':maskWidth,
    'height':maskHeight
});

var windowH = Math.round(winH/2-$(id).height()/2) + '.px';
var windowW = Math.round(winW/2-$(id).width()/2) + '.px';

$(id).css({
    'top': windowH,
    'left': windowW
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM