从后面的代码调用时,Javascript函数不起作用(C#)

时间:2015-12-07 16:29:23

标签: javascript c#

我有这个javascript函数:

<script type="text/javascript">
        function montaDataSubstituicaoPrestador(dt_exclusao) {

            var arrData = dt_exclusao.split('/');
            var exclusaoFormatada = arrData[1] + '-' + arrData[0] + '-' + arrData[2];
            var dias = parseInt(prazoSubPrestador);
            var novaData = new Date(arrData[2], arrData[1] - 1, arrData[0]);

            novaData.setDate(novaData.getDate() + dias);

            hoje = new Date(novaData)
            dia = hoje.getDate()
            mes = hoje.getMonth()
            ano = hoje.getFullYear()
            if (dia < 10)
                dia = "0" + dia

            if ((mes + 1) < 10)
                mes = "0" + (mes + 1);

            if (ano < 2000)
                ano = "19" + ano

            var dt = dia + "/" + (mes) + "/" + ano;

            document.getElementById('lblPrazoSubsAns').innerHTML 
                                             = "Prazo de substituição: " + dt;
        }
    </script>

我从后面的代码中接到了这个电话:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(), 
         "MontaDataExclusaoPrazoANS", 
         "montaDataSubstituicaoPrestador(" + calDataExclusao.Date + ")", true);

这不起作用。什么都没发生。我该怎么办?

如果我这样做,请参考,但参数为null:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
               "_montaDataSubstituicaoPrestador", 
               "montaDataSubstituicaoPrestador(null);", true);

但是,如果改变不起作用。不接受连接字符:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
               "_montaDataSubstituicaoPrestador", 
               "montaDataSubstituicaoPrestador(" + nova_data + ");", true);

这样,我有这个错误:

  参数列表

之后的

Uncaught SyntaxError:missing)

为什么?

2 个答案:

答案 0 :(得分:0)

尝试从

更改您的功能
ClientScriptManager cs = Page.ClientScript;
            cs.RegisterClientScriptBlock(this.GetType(), "MontaDataExclusaoPrazoANS", "montaDataSubstituicaoPrestador(" + calDataExclusao.Date + ")", true)

ClientScriptManager cs = Page.ClientScript;
            cs.RegisterClientScriptBlock(this.GetType(), "MontaDataExclusaoPrazoANS", "montaDataSubstituicaoPrestador('" + calDataExclusao.Date + "')", true)

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador(" + nova_data + ");", true);

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador('" + nova_data + "');", true);

因为javascript不能将直接参数理解为参数,所以在连接时必须用单引号指定它​​。  希望它能起作用

答案 1 :(得分:0)

试试这个:

protected void btnSUbmit_Click(object sender, EventArgs e) { string check = "hello"; Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "montaDataSubstituicaoPrestador('" + check + "')", true); }