ASP.NET Jquery C#MessageBox.Show对话框呃...问题 [英] ASP.NET Jquery C# MessageBox.Show dialog uh...issue

查看:299
本文介绍了ASP.NET Jquery C#MessageBox.Show对话框呃...问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个ASP.NET网站,而且我正试图通过jQuery获得更好的对话框。 Web应用程序有一个名为MessageBox的C#类,它允许从服务器端向客户端显示消息....基本上在aspx代码隐藏的C#中,如果某些逻辑不计算,则可以只是MessageBox.Show( '你的错误消息');

I am maintaining an ASP.NET site, and I was attempting to get the dialogs looking better using jQuery. The web application has a C# class called MessageBox which allows messages to be shown to the client from the server side.... essentially in the C# on an aspx codebehind if some logic 'does not compute', you can just MessageBox.Show('your error message');

由于MessageBox类似乎只是注入javascript ...警报(你的消息)我尝试将javascript更改为一个jquery对话框调用:

Since the MessageBox class appeared to just 'inject' javascript...the "alert(your message)" I tried changing the javascript to a jquery dialog call:

html:标准的jQuery示例对话框...(切断标签的目的只是为了获得代码示例显示。 ..这可能是一个真正的方式来做这个在这里...但这是我的第一篇文章...)

html: the standard jQuery example dialog... (cut off the tags on purpose...just to get the code example to show up... there is probably a real way to do this on here... but this is my first post...)


div id="dialog" title="Example dialog">
 p>Some text that you want to display to the user./p>
/div>

jQuery:
我注释了警报,并替换:
sb.Append($('dialog')。dialog('open'););

jQuery: I commented out the Alert, and substituted: sb.Append("$('dialog').dialog('open');");


while( iMsgCount-- > 0 )
{
  sMsg = (string) queue.Dequeue();
  sMsg = sMsg.Replace( "\n", "\\n" );
  sMsg = sMsg.Replace( "\"", "'" );
  //sb.Append( @"alert( """ + sMsg + @""" );" );

  **** sb.Append("$('dialog').dialog('open');"); ****
}

我希望这可以打开html中设置的对话框,但没有显示
我认为javascript是javascript ...而是执行一个jQuery呼叫与手动警报并不重要,但是显然有一个断开连接。

I was expecting this to open the dialog set up in html, however nothing shows. I figured javascript is javascript... and that executing instead a jQuery call versus manual Alert wouldn't matter... however clearly there is a disconnect.

有关如何解决这个问题的任何想法?不知道?

Any thoughts on how to solve this problem? Or any better implementations out there I am not aware of?

谢谢,任何和所有的帮助...我已经包括完整的MessageBox类下面。

Thanks, for any and all help... I've include the full MessageBox class below.

Curt。


public class MessageBox
{
    private static Hashtable m_executingPages = new Hashtable();

 private MessageBox(){}

    public static void Show( string sMessage )
    {
       if( !m_executingPages.Contains( HttpContext.Current.Handler ) )
       {
          Page executingPage = HttpContext.Current.Handler as Page;
          if( executingPage != null )
          {
             Queue messageQueue = new Queue();
             messageQueue.Enqueue( sMessage );
             m_executingPages.Add( HttpContext.Current.Handler, messageQueue );
             executingPage.Unload += new EventHandler( ExecutingPage_Unload );
          }   
       }
       else
       {
          Queue queue = (Queue) m_executingPages[ HttpContext.Current.Handler ];
          queue.Enqueue( sMessage );
       }
    }

    private static void ExecutingPage_Unload(object sender, EventArgs e)
    {
       Queue queue = (Queue) m_executingPages[ HttpContext.Current.Handler ];
       if( queue != null )
       {
          StringBuilder sb = new StringBuilder();
          int iMsgCount = queue.Count;
          sb.Append( "" );
          string sMsg;
          while( iMsgCount-- > 0 )
          {
             sMsg = (string) queue.Dequeue();
             sMsg = sMsg.Replace( "\n", "\\n" );
             sMsg = sMsg.Replace( "\"", "'" );
             sb.Append( @"alert( """ + sMsg + @""" );" );
          }

          sb.Append( @"" );
          m_executingPages.Remove( HttpContext.Current.Handler );
          HttpContext.Current.Response.Write( sb.ToString() );
       }
    }
 }


推荐答案

这是奇怪的...我写了一个类几乎相同的很久以前,一秒钟我以为你在使用它!

this is bizarre... I wrote a class almost identical a long time ago. for a second I thought you were using it!

无论如何,我挖了代码我使用了它,它允许你指定一个回调函数名,以防你不想使用alert功能。

anyway, I dug up the code from mine. I've used it quite a bit. It allows you to specify a "Callback" function name in case you want to not use the "alert" functionality.

btw,您需要注意静态Hashtable ,如果您有多个人同时使用该应用,则可能会收到对方的消息。

btw, you need to be careful with the static Hashtable. if you have multiple people using the app at the same time, they might get each other's messages.

用法:

<webapp:MessageBox ID="messageBox" Callback="showMessage" runat="server" />
<script type="text/javascript">
    function showMessage(messages) {
        $("#dialog p").empty();
        for(var msg in messages) {
            $("#dialog p").html += msg;
        }
        $("#dialog p").show();
    }
</script>

我没有测试回调脚本,但是你得到了这个想法。

I didn't test the callback script, but you get the idea.

和代码:

/// <summary>
/// MessageBox is a class that allows a developer to enqueue messages to be
/// displayed to the user on the client side, when the page next loads
/// </summary>
public class MessageBox : System.Web.UI.UserControl
{

    /// <summary>
    /// queues up a message to be displayed on the next rendering.
    /// </summary>
    public static void Show( string message )
    {
        Messages.Enqueue( message );
    }

    /// <summary>
    /// queues up a message to be displayed on the next rendering.
    /// </summary>
    public static void Show( string message, params object[] args )
    {
        Show( string.Format( message, args ) );
    }

    /// <summary>
    /// override of OnPreRender to render any items in the queue as javascript
    /// </summary>
    protected override void OnPreRender( EventArgs e )
    {
        base.OnPreRender( e );

        if ( Messages.Count > 0 )
        {

            StringBuilder script = new StringBuilder();
            int count = 0;

            script.AppendLine( "var messages = new Array();" );

            while ( Messages.Count > 0 )
            {
                string text = Messages.Dequeue();
                text = text.Replace( "\\", "\\\\" );
                text = text.Replace( "\'", "\\\'" );
                text = text.Replace( "\r", "\\r" );
                text = text.Replace( "\n", "\\n" );

                script.AppendFormat( "messages[{0}] = '{1}';{2}", count++, HttpUtility.HtmlEncode(text), Environment.NewLine );
            }

            if ( string.IsNullOrEmpty( Callback ) )
            {
                // display as "alert"s if callback is not specified
                script.AppendFormat( "for(i=0;i<messages.length;i++) alert(messages[i]);{0}", Environment.NewLine );
            }
            else
            {
                // call the callback if specified
                script.AppendFormat( "{0}(messages);{1}", Callback, Environment.NewLine );
            }

            Page.ClientScript.RegisterStartupScript( this.GetType(), "messages", script.ToString(), true );
        }
    }

    /// <summary>
    /// gets or sets the name of the javascript method to call to display the messages
    /// </summary>
    public string Callback
    {
        get { return callback; }
        set { callback = value; }
    }
    private string callback;

    /// <summary>
    /// helper to expose the queue in the session
    /// </summary>
    private static Queue<string> Messages
    {
        get
        {
            Queue<string> messages = (Queue<string>)HttpContext.Current.Session[MessageQueue];
            if ( messages == null )
            {
                messages = new Queue<string>();
                HttpContext.Current.Session[MessageQueue] = messages;
            }
            return messages;
        }
    }
    private static string MessageQueue = "MessageQueue";

}

这篇关于ASP.NET Jquery C#MessageBox.Show对话框呃...问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆