Saturday, March 24, 2012

Ajax and Response.Write

It seems like you cannot use Ajax on a page which has response.write on it. Did anyone find a solution to this problem?

Thanks,

jsmith

It's not a problem; I've done it. Can you post code?


I am having the same problem displaying a modla dialog. It used to work, but when the panel that contains the button that ran the code to display the modal dialog is wrapped in an update panel, it quit working.

Private Sub btnShowSpargerParms_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowSpargerParms.Click
MakeModelInputTable()
Response.Write("<script language='javascript'>showModalDialog('SpargerChooser.aspx', null,'status:no;dialogWidth:400px;dialogHeight:650px;dialogHide:false;help:no;scroll:no');</script>")
End Sub

MakeModelInputTable gets some parameters from the current page, puts them into a session variable, then I display the modal dialog which does some calculations in the pageload using the parameters in the session variable, then displays the results on the modal dialog.


You can use theRegisterStartupScript methodin thisPOST,

and showModalDialog in the same way.

Regards.


Thanks, it is working now. This is what the new code looks like:

MakeModelInputTable()
ScriptManager.RegisterStartupScript(updAgitData, updAgitData.GetType, "key", "<script language='javascript'>showModalDialog('SpargerChooser.aspx', null,'status:no;dialogWidth:400px;dialogHeight:650px;dialogHide:false;help:no;scroll:no');</script>", False)


Here is a discussion of the error:

http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/

But I cannot use the solution discussed here.

jsmith


js2004:

Here is a discussion of the error:

http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/

But I cannot use the solution discussed here.

jsmith

Response.Write not support with ASP.NET AJAX .You can use Scriptmanager mothod like the post before.Or put your text in a Lable,"This suggestion from the blog you put it up".Regards.

js2004:

Here is a discussion of the error:

http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/

But I cannot use the solution discussed here.

jsmith

Response.Write not support with ASP.NET AJAX .You can use Scriptmanager mothod like the post before.Or put your text in a Lable,"This suggestion from the blog you put it up".Regards.

That is right I can use ScriptManger.RegisterStartupScript(or RegisterClientScriptBlock). But that script gets written in page when it is loaded.

But that does not meet requirements if I have to dynamically write javascript during an ajax method call.

Consider the case: Trying to use Ajax to handle the OnMenuItemClick event of Asp:Menu, I need to warn users through a dialog box that if they navigate away from the page their data will be lost. Easy solution will be to do a Response.Write(confirm) in the event. But because of the inability of Ajax and Response.Write to work together I had to do a lot of twists to get the same effect. Even if I write javascript to a literal control and add it to page at run time I get the same error. I am not sure if I write javascript to a label it will work.

That is why I was wondering if there is a way to use Ajax and Response.Write at the same time in a page.

Best Regards,

jsmith


paul.vencill:

It's not a problem; I've done it. Can you post code?

Have you used Ajax.net v1.0 and Response.Write together?

Regards,

jsmith


You can use this way:

protected void Menu1_MenuItemClick1(object sender, MenuEventArgs e) { ScriptManager.RegisterStartupScript(this,this.GetType(),"key","confirm('Are you sure you want to delete?');",true); }

I think what is happening is this: (but won't bet my life on it!)

When you put something in your code-behind page that renders javascript, Ajax is controlling javascript at this point and it won't let anything else get rendered, UNLESS you register the script in your code behind page.

Registering the script lets Ajax know that javascript is being rendered, so Ajax know what to do with it.

I wish one of the Microsquish weenies would respond to this.

fcedotal


The response from the server from an async postback has to have a particular format so that the AJAX Library running in the browser can parse it. Response.Write changes the format of the response, making it unparsable. That's why Response.Write is incompatible with asynchronous postbacks.

For examples such as the one in this thread, ScriptManager.RegisterStartupScript(...) is usually what you need.

Yours Truly,
Steve Marx
Microsquish Weenie


Steve Marx:

The response from the server from an async postback has to have a particular format so that the AJAX Library running in the browser can parse it. Response.Write changes the format of the response, making it unparsable. That's why Response.Write is incompatible with asynchronous postbacks.

For examples such as the one in this thread, ScriptManager.RegisterStartupScript(...) is usually what you need.

Yours Truly,
Steve Marx
Microsquish Weenie

Ok. Thank you Steve.

jsmith

No comments:

Post a Comment