neha dhage
neha dhage

Reputation: 11

how to write response.write within update panel

i am calling a function which is inside Homescroll.ascx.cs from Homescroll.ascx

so i wrote on Homescroll.ascx as <% Response.Write(scroll()); %>

but all this is in update panel,and i am getting errors. so is their any other way to call function from homescroll.ascx to homescroll.ascx.cs,instead of response.write();

Upvotes: 1

Views: 6276

Answers (6)

Carlos Figueroa
Carlos Figueroa

Reputation: 1

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append(@"<script type='text/javascript'>");
sb.Append("alert('Usuario agregado Correctamente.');");      
sb.Append(@"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AddHideModalScript", sb.ToString(), false);

Upvotes: 0

Filip Zivanovic
Filip Zivanovic

Reputation: 1

A simple solution to this problem I use is to call jQuery function and to append() HTML instead of calling Response.Write(). For example, let's say I wanna update some HTML text inside UpdatePanel and change its Text I would do something like this:

With Response.Write(), it would be simple as this:

Response.Write("[TextToBeAddedToHTML]");

But with jQuery, it's a little complicated, and you have to include jQuery library to HTML page:

ScriptManager.RegisterStartupScript(this, GetType(), "TextUpdate", "$(\"#[ID_OF_HTML_Element]\").append(\"<p>" + [TextToBeAddedToHTML] + "</p>\");", true);

Upvotes: 0

user2000095-tim
user2000095-tim

Reputation: 446

Reponse.Write breaks the output in an UpdatePanel, I blogged about a possible solution here: http://timscyclingblog.wordpress.com/2013/03/07/asp-net-web-forms-response-write-in-an-updatepanel-dev-web/ Hope it helps somebody else in the future, though it is slightly off topic given other solutions which remove the requirement to write to the Response.

Upvotes: 0

Michael Kropat
Michael Kropat

Reputation: 15217

<%= scroll() %> will work in an UpdatePanel, but <% Response.Write(scroll()) %> won't.

Contrary to popular belief, <%= %> displaying-expressions don't precisely mean Response.Write, not like they did in asp classic. They both execute during the render phase. The difference is that displaying-expressions write to the HtmlTextWriter passed to the render function—not directly to the Response object. Normally this doesn't make any difference. Except when you're using UpdatePanel, which needs to capture the output of your ContentTemplate, so that it can encode the output in its special format. If you call Response.Write directly, you bypass the encoding performed by UpdatePanel and mess up the response so that it can't be parsed on the client side.

The error that occurs when you do this is unusually helpful:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

The author of UpdatePanel provides workarounds on his blog.

Upvotes: 4

Mike
Mike

Reputation: 5509

Need more information but I would assume scroll would be a client function just by its name and you should use a scriptmanager like this:

ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"scrollScript","scroll();",true);

see http://msdn.microsoft.com/en-us/library/bb350750.aspx for more info. If you don;t want to call it from the code behind there is no need for the <% %> or the respoonse.write at all. Just wrap it in a script tag with the type set to text/javascript.

Upvotes: 0

VinayC
VinayC

Reputation: 49195

How about using syntax - <%= scroll() %>?

Upvotes: 1

Related Questions