等待ClientScript.RegisterStartupScript完成

Wait until ClientScript.RegisterStartupScript is finished

本文关键字:完成 RegisterStartupScript ClientScript 等待      更新时间:2023-09-26

我正在将JavaScript代码转换为ASP。. NET代码,但我遇到了一个问题。我仍然使用JS代码向用户显示提示窗口(使用JS代码在后面的代码)。

JS代码被执行,用户的输入被保存在一个隐藏字段中。在代码之后,我从hiddenfield读取值,并将其保存在字符串中以供以后使用。

问题是读取hiddenfield值的代码在JS代码执行之前被执行,这导致一个空字符串。

我怎么能让我的代码什么,直到JS代码已经完全执行?

ClientScript.RegisterStartupScript(this.GetType(), "prompt", "var value = prompt('Enter your password here.'); storeinput(value);", true);
    string code = hidValue.Value;
    Debug.WriteLine("Password: " + code);

谢谢!

把ScriptManager放到asp.net页面中,像这样

<asp:ScriptManager ScriptMode="Release" LoadScriptsBeforeUI="false" EnablePageMethods="true"
    runat="server" ID="ScriptManager1" />

在页面中创建方法并装饰[WebMethod]属性

[WebMethod]
public static void GetPasswordFromClient(string password) {
    Debug.WriteLine("Password: " + password);
}

并在javascript中编写以下代码:

function OnSucceeded_SendPasswordToServer(result, userContext, methodName) {
}
function OnFailed_SendPasswordToServer(error, userContext, methodName) {
    alert(error);
}
function SendPasswordToServer(string password) {
    PageMethods.GetPasswordFromClient(password, OnSucceeded_SendPasswordToServer, OnFailed_SendPasswordToServer); 
}

,现在你可以使用注册脚本或在你的页面使用:

var value = prompt('Enter your password here.'); 
SendPasswordToServer(value);