简体   繁体   English

使用asp.net的Javascript警报

[英]Javascript alert using asp.net

I want to bring up a js alert box on asp.net button click. 我想在单击asp.net按钮时调出一个js警报框。 The code i have is 我的代码是

 String jscript = @"<script language = 'javascript'> alert('This is my title');</script>";
 ClientScript.RegisterStartupScript(GetType(), "_jscript", jscript);

it works fine, but I want to have some more js popups later for some validation, where I assume I have to write the same code, but unfortunately it does not load rest of the popups on the same page. 它工作正常,但我想稍后再有更多js弹出窗口进行验证,我假设我必须编写相同的代码,但不幸的是,它不会在同一页面上加载其余的弹出窗口。

Is this to do with update panels? 这与更新面板有关吗?

The control I am validating is 我正在验证的控件是

if (dp_menu.SelectedIndex > 0)
{
   //continue program
}
else
{
  //show popup
  //this pop p doesn't show up at all?
  String jscript = @"<script language = 'javascript'> alert('Another popup');</script>";
  ClientScript.RegisterStartupScript(GetType(), "_jscript", jscript);

}

Take a look at the specification for RegisterStartupScript , specifically the Remarks section: 看一下RegisterStartupScript规范 ,特别是“备注”部分:

A client script is uniquely identified by its key and its type. 客户端脚本由其键和类型唯一标识。 Scripts with the same key and type are considered duplicates. 具有相同键和类型的脚本被视为重复脚本。 Only one script with a given type and key pair can be registered with the page. 页面上只能注册具有给定类型和密钥对的一个脚本。 Attempting to register a script that is already registered does not create a duplicate of the script. 尝试注册已注册的脚本不会创建该脚本的副本。

If you're passing in the same type via GetType() and the same key, "_jscript" , every time, then only the first call will result in any <script> being rendered. 如果每次通过GetType()和相同的键"_jscript"传递相同的类型,则仅第一个调用将导致呈现任何<script> This is by design. 这是设计使然。

Poor Fix : Replace your unchanging key "_jscript" with a different key for each validation you perform, eg "_valNameIsBlank" , "_valNoItemSpecified" . 较差的解决方案 :对于您执行的每个验证,请将不变的密钥"_jscript"替换为其他密钥,例如"_valNameIsBlank""_valNoItemSpecified"

Better Fix : Avoid annoying your users with multiple validation popups by: 更好的解决方案 :通过以下方法避免用多个验证弹出窗口来烦扰用户:

  • Compiling a List<string> of all your validation failures 编译所有验证失败的List<string>
  • After all your checks, see if you have any items in the list 完成所有检查后,查看列表中是否有任何项目
  • If so, concatenate them into a single validation failure message and display that in a single alert. 如果是这样,请将它们串联成一条验证失败消息,并在一条警报中显示该消息。

i would give you some thing more effective, 我会给你一些更有效的方法,
costume control 服装控制

  1. create new class library (Library_name) 创建新的类库(Library_name)

add this class to library 将该类添加到库中

using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace ClientSide
{
    [DefaultProperty("Text"),
    ToolboxData("<{0}:MessageBox runat=server>" 
        + "</{0}:MessageBox>")]
    public class MessageBox : System.Web.UI.Control 
    {
        private string text="";
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public string Text
        {
            get {return text;}
            set {text = value;}
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (text.Length>0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<script language='javascript'>");
                sb.Append("alert('"+text+"')");
                sb.Append("</script>");
                output.Write(sb.ToString());
            }
        }
    }
}

to use it you have to register the control in the top of aspx page 要使用它,您必须在aspx页面顶部注册控件

<%@ Register TagPrefix="cc1" Namespace="ClientSide" Assembly="Library_Name" %>

3- then you can use it like this way in the aspx page 3-然后您可以在aspx页面中以这种方式使用它

<cc1:MessageBox id="MessageBox1" runat="server" Text="popup Message"></cc1:MessageBox>

In RegisterStartupScript Keyvalues should be different for each script 在RegisterStartupScript中,每个脚本的键值应不同

Try different KeyValues instead of using "_jscript" in all script. 请尝试使用不同的键值,而不要在所有脚本中都使用“ _jscript”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM