我通过
显示警报ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert",
"alert('" + ex.Message.Replace("'", "") + "');", true);
在服务器端按钮的 catch block 中单击。但我在按钮单击的第一行中调用另一个脚本管理器为
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert",
"$('#ContentPlaceHolder1_ctl00_rwsender').show();
ValidatorEnable(document.getElementById('ContentPlaceHolder1_ctl00_rfvsender'), true);", true);
在这种情况下,它不会显示警报消息,但第一行脚本正在运行。
如果我删除第一行脚本代码,警报就会起作用。两个不同的脚本管理器之间有什么关系?为什么一个脚本会影响另一个脚本? 我该如何解决这个问题?
谢谢
最佳答案
第二个不起作用,因为你直接向脚本管理器发送Js指令,(注意alert是窗口 native 函数,这就是它起作用的原因)
只需在客户端(aspx)中声明一个函数:
function ShowElement(){
$('#ContentPlaceHolder1_ctl00_rwsender').show();
ValidatorEnable(document.getElementById('ContentPlaceHolder1_ctl00_rfvsender'), true);
}
然后在后端最后调用这个:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "show",'ShowElement()', true);
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert","alert('" + ex.Message.Replace("'", "") + "');", true);
请注意,每个注册脚本的 key 必须是唯一的, 这里第一个注册为“show”和警报“alert”
仅表单信息可防止在 js 代码中使用 sush 生成的 cleint ID :
只需使用 document.getElementById("<%=NameOfYourControl.ClientID %>"), true);
而不是硬编码值:ContentPlaceHolder1_ctl00_rwsender
.
关于javascript - 从后面的代码调用警报不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733019/