javascript - 从后面的代码调用警报不起作用

标签 javascript c# asp.net

我通过

显示警报
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/

相关文章:

javascript - 获取动态添加的输入字段的内容

html - 保持像素比但缩放尺寸

javascript - 在 Meteor 的 Firefox 中登录不起作用

javascript - Ember.js/Ember-数据 : Using the RESTAdapter to load data from multiple endpoints

javascript - 多状态按钮,如 angularJS 中的切换

c# - 尽管将 ContentPlaceHolder 和 <div> 设置为宽度 :100%,但内容超出了屏幕

asp.net - 在依赖 ConfigurationManager 的 .net core 2 中引用 .net 框架程序集

javascript - react : Do children always rerender when the parent component rerenders?

c# - Microsoft Visual Studio C# 2010 Express : Can debug but not build

c# - 通过 json 传递 datetime 时 API 对象为 null