当用户从下拉列表
列表中选择数据时,我需要从代码隐藏调用确认消息框,并且当所选数据为1时,例如,将向用户显示一个确认框以确认其操作
所以我在调用此 JavaScript 方法的背后代码中执行了以下操作:
if (dropdownlist1.SelectedValue == 1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "CallConfirmBox", "CallConfirmBox();", true);
}
脚本函数:
<script type="text/javascript">
function CallConfirmBox() {
if (confirm("هل تريد ان تفصل الباليت؟")) {
alert("سيتم فصل الباليت!");
PageMethods.getdata(onSuccess, onError);
function onSuccess() {
alert(data);
}
function onError() {
alert(errorMessage);
}
}
} else {
//CANCEL – Do your stuff or call any callback method here..
alert("done!");
}
}
我在 HTML 代码的开头添加了以下行:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
这是从脚本调用的函数背后的代码:
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static void getdata()
{
int nRowsCheck = cMDP.Update_Segregation_PalletPart(nPalletNo);
if (nRowsCheck != 0)
{
nRowsCheck = 0;
nRowsCheck = cMDP.Update_Segregation_Pallet(nPalletNo, nUserID);
if (nRowsCheck != 0)
{
nRowsCheck = 0;
nRowsCheck = cMDP.Delete_Segregation_PalletPart_Delete(nPalletNo);
if (nRowsCheck != 0)
{
nRowsCheck = 0;
nRowsCheck = cMDP.Delete_Segregation_Pallet_Delete(nPalletNo);
}
}
}
}
但是我遇到了以下错误:
Page Methods is undefined when run the script !!
请帮忙,因为我需要一些支持
最佳答案
首先,您必须在 JavaScript 中的 else
之前删除一个 }
。
更改隐藏代码:
if (dropdownlist1.SelectedValue == "1")
对于主要问题:页面方法未定义
:
从您的评论看来,您正在使用用户控件(ascx)。 页面方法不能在用户控件中使用。请引用以下问题:
最简单的解决方案是使用 aspx WebForm 而不是 ascx 用户控件。这就是我已经测试和工作过的。
或者您可以使用 WebService,如以下问题中所指定:
但是示例的链接不再有效。
或者您可以尝试使用此项目,尝试将 ASP.NET AJAX 页面方法引入 UserControls:
关于javascript - 从代码隐藏和代码隐藏中的脚本调用函数内部调用 JavaScript(PageMethods 未定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827113/