2012年3月26日月曜日

return alert msg on ajax call

Hello guys

Quick question. Scenario: I have a update button that does an AJAX call and updates DB. I want to return a success/failure message such that it shows up as an alert message box on UI rather than showing a lblMessage and updating its visiblity and text property. Any suggestions?

Thanks in advance.

Hi There,

Try following:

protectedvoid Button1_Click(object sender,EventArgs e)

{

// Alert with messagebox after callback

// Your process here e.g. add/edit/delete from database and etc

// Assume your process take 5 second to complete

System.Threading.Thread.Sleep(5000);

// Build your alert script

StringBuilder sbScript =newStringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>");

sbScript.Append("alert('hello world!')");

sbScript.Append("</script>" );

// Make use ScriptManager to register the script

ScriptManager.RegisterStartupScript(this,this.GetType(),"@.@.@.@.MyCallBackAlertScript", sbScript.ToString(),false);

}


Hi,

You can write a javascript function taking a string parameter on your page having an alert function call displaying the message in the parameter. I have posted a similar solution in this forum. You can have a look at that. I hope you find it useful. It is at http://forums.asp.net/t/1122462.aspx .

If you have any problem in that, let us know.



Thanks for this d4dennis..it worked..but I am not happy man yet. I have two questions..first, on postback is this still sitting on the client page. If so, is there any chance it might called again unwantedly?

Second, I was hoping to trap the Sys object of ajax. I have seen that when there is an exception thrown, it shows up as an alert message on screen. I want to somehow be able to trap that guy. Any ideas there? sorry for being so picky but that's what I want.

This works and right now to continue, I am using this. thanks again.


Hi There,

This is the solution i can think of.

tinuverma:

First, on postback is this still sitting on the client page. If so, is there any chance it might called again unwantedly?

THIS WILL NOT POP UP ON POSTBACK

tinuverma:

Second, I was hoping to trap the Sys object of ajax. I have seen that when there is an exception thrown, it shows up as an alert message on screen. I want to somehow be able to trap that guy. Any ideas there? sorry for being so picky but that's what I want.

IM NOT SURE THIS IS ACHIEVEABLE, IF SOMEONE HAVE ANY IDEA. ANYWAY, YOU CAN RAISE THIS AS ANOTHER QUESTION.

0 件のコメント:

コメントを投稿