我对于在同一个应用程序中结合使用 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 和 FireBug for firefox ( http://getfirebug.com/ ) - 这将为您提供一个很好的 Javascript 开发调试器,并立即向您显示该错误。
关于c# - 从 C# 代码隐藏 (.NET) 调用 JavaScript 函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16798927/