从c#调用javascript函数不起作用

从c#调用javascript函数不起作用,c#,javascript,jquery,C#,Javascript,Jquery,大家好,我的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).wi

大家好,我的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();
});     
我想显示c#按钮点击事件的弹出窗口,如下所示:

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>

问询处
这是我的最爱。Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante。
警惕
这是我的最爱。Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante。
验证
这是我的最爱。Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante。
要求取消确认
这是我的最爱。Mauris congue sodales risus ac cele risque Etiam vel nisl ante.Etiam vel nisl ante。

警报未显示的pb是什么?您传递给javascript函数的参数似乎缺少引号:

线路

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

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

在afficherMessageInfo()之后缺少分号


您是否尝试过使用浏览器工具对此进行调试?

删除
RegisterStartupScript
函数调用中的
true
参数,原因是它已经在标记中定义,并且肯定在
块中

RegisterStartupScript
有一个重载,该重载不会在末尾使用
include脚本块参数

因此,请将您的行更改为:

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);

}

此方法将阻止您进行
回发
,并立即弹出一个窗口。

Hi lcarus。该函数在popupscript.js中定义file@user594166但是您的标记上仍然有一个
。尝试删除true参数并尝试一下。RegisterStartupScript有一个重载,它没有在末尾使用
包含脚本块参数
。你能在回答中写下这个lcarus吗?谢谢你,lcarus。它只将“afficherMessageInfo('#info','le dossier aétécrée avec success')”添加到DOM(在文档底部,afficherMessageInfo('#info',它这样写)“le dossier aétécrée avec Success'))这是bizzareit附加的“afficherMessageInfo('info','le dossier aétécrée avec Success')“到文档末尾。此代码存在很多问题。对于原始海报,是否可以雇佣懂Javascript和C的人?我这样问是因为,例如,您的字符串正在被传递,而未被转移,这使得它们对脚本注入是开放的。让这一页正常工作并不是你唯一的挑战。pb如何处理代码Jason。这与everywhere检查的代码相同:你的代码背后发出的javascript中有许多语法错误。调试javascript的最佳方法是在chrome中运行它,并打开开发人员工具以确定导致错误的原因。
// 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
});