从c#代码后面传递函数参数

Pass function parameter from c# code behind

本文关键字:传递函数 参数 代码      更新时间:2023-09-26

我有一个ASP.NET Web应用程序项目。在该项目中,我想从c#代码隐藏页向javascript中的函数传递值。

我的C#代码段:-

//Here td_Output is TableCell
td_Output.Text = "<input type='button' style='font-size:11px;' onclick='LoadLoayaltyProgram(1,'"+Convert.ToString(Request.Form["Program"])+"');' class='ui-button ui-widget ui-state-default ui-corner-all' role='button' value='Add' />";

JavaScript代码:-

function LoadLoayaltyProgram(PID,ID) {
    alert(PID +" "+ ID);
}

当我在浏览器中的.aspx页面上检查元素时,我看到的代码类似:

<input type="button" style="font-size:11px;" onclick="LoadLoayaltyProgram(1," pid112002');'="" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" value="Add">

单击"添加"按钮时我没有收到任何警报

LoadLoayaltyProgram中,您正在打印连续的'。所以,用'"来转义",这样你就可以用类似这样的东西来重用它:

string program = Server.UrlEncode(Request.Form["Program"] ?? string.Empty);
td_Output.Text = String.Format("<input type='button' style='font-size:11px;' onclick='LoadLoayaltyProgram(1, '"{0}'");' class='ui-button ui-widget ui-state-default ui-corner-all' role='button' value='Add' />", program);
  onclick="LoadLoayaltyProgram(1," pid112002');'="" 

这个看起来很奇怪,不是应该是什么样子吗?

  onclick="LoadLoayaltyProgram(1, 'pid112002')"