c# - 将参数从代码隐藏传递给 javascript 函数,错误

标签 c# javascript asp.net argument-passing

在后面的代码中:

    string func = "showSuccessMessage("+name+");";
    ClientScript.RegisterStartupScript(this.GetType(), "success", func, true);

在 js 文件中:

   function showSuccessMessage(user)
   {
        $("#box").dialog({
            title:"User Registration",
            html: user.toString() + " Successfully Registered", 
            modal: true,
            buttons: {
                   Ok: function () {
                    $(this).dialog("close");
                    var s = "../Pages/main.aspx";                
                    window.location = s;
                   }
        }
   });
 } // end function 

当我尝试传递不带参数的 func 字符串时,一切正常

当我尝试将参数附加到函数字符串中时,我从浏览器中得到一个错误

未捕获的 ReferenceError:john 未定义

(john 是名字的值) 现在我猜问题的发生是因为函数注册的方式无法区分变量和值,所以它是一个未定义的类型 john

所以问题成立:

如何从代码隐藏向 javascript 函数发送参数

提前致谢 艾伦。

顺便说一句类似的问题: 显示类似于我尝试过的内容

similar question

最佳答案

您编写它的方式就像将 john 作为变量而不是字符串传递。这就像调用

showSuccessMessage(john); // what you're doing in js
showSuccessMessage('john'); // what you need

更正

string func = "showSuccessMessage('"+name+"');"; 

关于c# - 将参数从代码隐藏传递给 javascript 函数,错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7779044/

相关文章:

asp.net - Silverlight 图像查看器控件建议

c# - 将List<>写入数据库asp.net

c# - Image.Save() 抛出异常 "Value cannot be null./r/nParameter name: encoder"

c# - 如何在 c# 中使用 PDFSharp 在 pdf 中嵌入数字签名?

javascript - 多个泛型参数类型推断不起作用

Javascript:按数字属性对对象数组进行排序,其中包括未定义的属性

c# - 无法在 asp.net 报表查看器中查看 SQL Server 移动报表

C# 应用程序在控制台中使用另一个应用程序

c# - Access SQL 查询缺少更多必需的参数

javascript - 为什么gapi.client.init(Google Drive API)不起作用?