0

在 Web 表单项目中,我需要打开警报。

我试着这样做

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "text", "SignOffAlert");
                            }

并在视图上添加 js-function

function SignOffAlert() {
            alert('The form cannot be submitted!');
        }

按钮单击警报不存在


更新 :

这段代码对我有用

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "signOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "signOffAlert", "alert('The form cannot be submitted!');", true);
                            }
4

2 回答 2

0

你需要注册完整的脚本,我相信

你看过这里的例子吗?http://msdn.microsoft.com/it-it/library/bahh2fef%28v=vs.110%29.aspx

于 2014-11-28T15:35:29.510 回答
0

试试这个,在 Page_Load 中写下这个:

string myscript = "  <script> function SignOffAlert() { alert('The form cannot be submitted!');}</script>";

    if (! Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "text", myscript);
    }

然后在表格中

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="SignOffAlert()" />
于 2014-11-28T16:33:31.937 回答