 |
|
 |
Excuse me.. I used in my apps (I kept your name there)
|
|
|
|
 |
|
|
 |
|
 |
I get the following error when I use this with AJAX. Is there a way around this problem?
Sys.Webforms.PagerequestManagerParserErrorException
Any help is appreciated!!
|
|
|
|
 |
|
 |
sb.Append("<script language="'javascript'">");
i tried the code but its showing some error.the error is too many characters in character literal.how to debug this
|
|
|
|
 |
|
 |
First of all, I would like to thank Lee, nice and useful class
And then,
I used this MessageBox class in my application and it works fine.
But I also need the Confirmation Message Box, for this reason I changed the code a little.
I made some changes in "ExecutingPage_Unload" method. Since I want to
get the answer of the user from the C# code, I added "true" for Ok, and "false" for Cancel to web page's Url. This way I will check if it is true or false and decide which button the user has clicked. And it works fine, but the problem is, since the JavaScript code is executed last and the ConfirmationMessageBox is clicked after everything in the ".cs file" is executed, I can not get the true/ false values from the URL.
I just want, the ConfirmationMessageBox to be shown, and get the true/false values from the URL exactly when reaching the "ConfirmationMessageBox.Show()" statement in the .cs file, and then continue with the rest of the code, but it executes "ExecutingPage_Unload" not when I need, but at the end.
Can you please help me on this issue?
Thanks...
The ConfirmationMessageBox Class is shown below:
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Text;
namespace XXX.Components
{
public class ConfirmationMessageBox
{
private static Hashtable m_executingPages = new Hashtable();
private ConfirmationMessageBox() { }
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("<script language="'javascript'">");
string sMsg;
while (iMsgCount-- > 0)
{
sMsg = (string)queue.Dequeue();
sMsg = sMsg.Replace("\n", "\\n");
sMsg = sMsg.Replace("\"", "'");
sb.Append(@"var answer = ");
sb.AppendLine(@"confirm( """ + sMsg + @""" );");
sb.AppendLine("if(answer){");
sb.AppendLine("var home_url = String(this.location).split(\"?\")");
sb.AppendLine("this.location = home_url[0].concat(\"?\",\"true\")}");
sb.AppendLine("else{");
sb.AppendLine("var home_url = String(this.location).split(\"?\") ");
sb.AppendLine("this.location = home_url[0].concat(\"?\",\"false\")}");
}
sb.Append(@"</script>");
m_executingPages.Remove(HttpContext.Current.Handler);
HttpContext.Current.Response.Write(sb.ToString());
}
}
}
}
|
|
|
|
 |
|
 |
Hi
I am trying to use MessageBox.show on click of a button which is inside update panel but I am not getting the alert message while I am trigger - postback on click of that button(I have tried async postback also i.e. not working also). I want to know, will it work inside update panel or not. Please guide me if I am wrong. Kindly help, Very Urgent.
Thanks Vivek
|
|
|
|
 |
|
 |
i faced same problem also. anyone got suggestion or solution? Thanks as advanced.
ma tju
Pengaturcara Perisian
Subang Jaya,Selangor, Malaysia
Ring Master SB MVP 2008
Subang Jaya MOP (Otai)
|
|
|
|
 |
|
 |
Hi ma tju,
I too faced the same problem but I got its solution. If you have to display the alert message inside update panel then after writing messagebox.show("My Message"), you should use server.transfer(samepage.aspx); This message is displayed at page unload event of current page from browser. Try this
like it
MessageBox.Show("Alert Message");
Server.Transfer("SameOrDifferentPage.aspx");
If you will not use Server.Transfer then message box will not appear.
Thanks
Vivek
Mark as an answer if your problem is solved.
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I was more into Windows Development, the Future of Web is Promising, So i recently started to move to web. You can include the namespace System.Windows.Forms to use the Messagebox that was nicely used in Windows development. I understand why you dont use it in your ASP.net. Do you think its a bad idea to use it? please explain. gave you 5
Thanks
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
 |
|
 |
We cannot use System.Windows.Forms.MessageBox in web application. If you try to use it then it misbehaves, it appears in the task bar, not on the parent browser. Then you have to click on task bar to show it over web form.
|
|
|
|
 |
|
 |
Thanks that is true, i see it misbehaves. I have started to enjoy
Response.write("<script> alert('hello world');</script>");
Thanks
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
 |
|
|
 |
|
 |
I am a .NET newbie. First, I want to thank you for this great article. It's very informative.
It would greatly support me in copying your code into my project to use it if you could add a line to the top of the listing for the MessageBox class:
using System.Collections;
Sincerely Yours,
Brian Hart
Department of Physics and Astronomy
University of California, Irvine
|
|
|
|
 |
|
 |
Hi Lee ,
I have one page using Ajax. I am calling MessageBox.Show("Show a Message"); But it shows error like
Sys.WebForms.PageRequestManagerParserErrorException: This message received from the server could not be parsed. Common causes for this error when the response is modified by calls to Response.Write(), reponse filters, HttpModules, or server trace is enabled.
Details: Error parsed near 'ntryDate"));
});
|<script language='ja'.
Thirumalai M
|
|
|
|
 |
|
 |
Anyone find a solution to the browser back button issue for this class.
|
|
|
|
 |
|
 |
tar565 wrote: Anyone find a solution to the browser back button issue for this class.
Just what are you referring to?
|
|
|
|
 |
|
 |
When you navigate away from a page after displaying a MessageBox and then press your browsers Back button, the MessageBox is displayed again.
|
|
|
|
 |
|
 |
Lee, just wanted to thank you for the awesome code. It is working great in my web app. Lucky for me the Code Project gets searched with the VS2005 Help.
|
|
|
|
 |
|
 |
I have tested it with ASP.NET 2.0 -> Imports Microsoft.VisualBasic Public Class MessageBox Private Shared m_executingPages As New Hashtable()
Private Sub New() End Sub 'New Public Shared Sub Show(ByVal sMessage As String) ' If this is the first time a page has called this method then If Not m_executingPages.Contains(HttpContext.Current.Handler) Then ' Attempt to cast HttpHandler as a Page. Dim executingPage As Page = HttpContext.Current.Handler ' 'ToDo: Error processing original source shown below ' ' '--------------------------------------------------------^--- Syntax error: ';' expected If Not (executingPage Is Nothing) Then ' Create a Queue to hold one or more messages. Dim messageQueue As New Queue() ' Add our message to the Queue messageQueue.Enqueue(sMessage) ' Add our message queue to the hash table. Use our page reference ' (IHttpHandler) as the key. m_executingPages.Add(HttpContext.Current.Handler, messageQueue) ' Wire up Unload event so that we can inject ' some JavaScript for the alerts. AddHandler executingPage.Unload, AddressOf ExecutingPage_Unload End If Else ' If were here then the method has allready been ' called from the executing Page. ' We have allready created a message queue and stored a ' reference to it in our hastable. Dim queue As Queue = CType(m_executingPages(HttpContext.Current.Handler), Queue) ' Add our message to the Queue queue.Enqueue(sMessage) End If End Sub 'Show ' Our page has finished rendering so lets output the ' JavaScript to produce the alert's Private Shared Sub ExecutingPage_Unload(ByVal sender As Object, ByVal e As EventArgs) ' Get our message queue from the hashtable Dim queue As Queue = CType(m_executingPages(HttpContext.Current.Handler), Queue) If Not (queue Is Nothing) Then Dim sb As New StringBuilder() ' How many messages have been registered? Dim iMsgCount As Integer = queue.Count ' Use StringBuilder to build up our client slide JavaScript. sb.Append("<script language='javascript'>") ' Loop round registered messages Dim sMsg As String While (iMsgCount > 0) iMsgCount = iMsgCount - 1 sMsg = CStr(queue.Dequeue()) sMsg = sMsg.Replace(ControlChars.Lf, "\n") sMsg = sMsg.Replace("""", "'") sb.Append("alert( """ + sMsg + """ );") End While ' Close our JS sb.Append("</script>") ' Were done, so remove our page reference from the hashtable m_executingPages.Remove(HttpContext.Current.Handler) ' Write the JavaScript to the end of the response stream. HttpContext.Current.Response.Write(sb.ToString()) End If End Sub 'ExecutingPage_Unload End Class 'MessageBox
|
|
|
|
 |
|
 |
or even better to use in stead of
sMsg = sMsg.Replace(ControlChars.Lf, "\n")
is
sMsg = sMsg.Replace(Environment.NewLine, "\n")
tmo
|
|
|
|
 |
|
 |
Thanks a lot for translating that and taking the time to share your code. Just what I was after
|
|
|
|
 |
|
 |
Could you give me a start on Yes No Message Box on ASP.Net.
Thanks
|
|
|
|
 |
|
 |
how do you use one MessageBox() class to accomodate other forms? is it possible?
|
|
|
|
 |