繁体   English   中英

从背后的C#代码调用JavaScript函数的问题(.NET)

[英]Issues calling JavaScript function from C# code behind (.NET)

我对同一应用程序中C#和javascript的结合还不陌生。 我认为我一定错过了使它们一起工作的一些重要步骤。 从我的代码背后调用我的javascript函数不会导致我期望的结果,但也不会导致错误。 根本没有任何反应。 我正在使用Visual Studio 2010进行开发,如果作为内置的JS调试器存在,我不知道在哪里可以找到它-无法单步执行会使情况变得更加糟糕。

在我的.aspx中(“ FieldName”值均来自代码的另一部分):

<script language ="javascript">
    var idSelection;
    var nameSelection;
    function selectRow(idItem, nameItem) {
        idSelection = idItem;
        nameSelection = nameItem;
        alert(idSelection + " " + nameSelection);
        var targetIdValue;
        var targetForm = window.opener.document.forms(0);
        eval("targetForm." + targetIdFieldName + ".value = '" + idSelection + "';");
        eval("targetForm." + targetNameFieldName + ".value = '" + nameSelection + "';");
        window.close();
    }
</script>

我的电话是:

        protected void AppGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
                txthidAppId = (HtmlInputHidden)Session["hidAppId_rvte"];
                txtAppName = (TextBox)Session["txtAppName_rvte"];

                txthidAppId.Value = selectedApp.Id;
                txtAppName.Text = selectedApp.Name;

                Page.ClientScript.RegisterStartupScript(GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);
}

无法完全确定要完成的工作,但是,Javascript中存在一些语法问题。

selectRow(“ + txthidAppId.Value +”,“ + txtAppName.Text +”)

应该

selectRow(“ + txthidAppId.Value +”,'“ + txtAppName.Text +”')

注意txtAppName.Text值周围的单引号' 您正在发送字符串,因此Javascript需要这样传递它们,否则它将它们视为对象。

我建议您安装用于Firefox的Firefox和FireBug( http://getfirebug.com/ )-这将为您提供一个不错的Java开发人员调试器,并立即向您显示该错误。

我没有发现任何错误。 我要做的是尝试在script标签中将language = javascript更改为type =“ text / javascript”。

另外,我会改变

Page.ClientScript.RegisterStartupScript(GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);

Page.ClientScript.RegisterStartupScript(Page.GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);

您必须在字符串参数中添加QuatationMarks。 我为此写了一个扩展方法。

    public static string AddQuatationMark(this string value)
    {
        string retStr = "";
        retStr = "" + "'" + value + "'" + "";
        return retStr;
    }

并在您的代码上使用此方法;

"selectRow(" + txthidAppId.Value.AddQuatationMark() + ", " + txtAppName.Text.AddQuatationMark() + ")"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM