craigmoliver
craigmoliver

Reputation: 6562

Response.Write like behavior when code is executed in an Update Panel

Is there a way to make Response.Write work in an UpdatePanel and not cause the app to error out for obvious reasons? Or, is there a way to get similar results without more than one line of code in C#?

Upvotes: 0

Views: 2385

Answers (5)

Filip Zivanovic
Filip Zivanovic

Reputation: 1

A simple solution to this problem I use is to call JQuery function html() insted calling Response.Write()... For example, lets say I wanna update some html text inside UpdatePanel I would do something like this:

With Response.Write(), it would be simple as this: Response.Write("[TextToBeAddedToHTML]");

But with Jquery its little complicated, and you have to include Jquery library to Html page:

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

Upvotes: 0

farfour
farfour

Reputation: 21

Just found this could be helpful to other people.

you cannot use Response.Write or Response.Redirect inside update panel.

To solve this you have to use Trigger. This is used to make a server trip from inside an update panel

<asp:UpdatePanel ID="UpdatePanel9" runat="server">
<Triggers>
    <asp:PostBackTrigger ControlID="btnExcel" />
</Triggers>
<ContentTemplate>

  ---Your code here

</ContentTemplate>

Here the ControlID is the button or other control. For example in the button click event you can write some text in the response.

Upvotes: 2

Jason
Jason

Reputation: 52533

Response.Write does not work with UpdatePanels, but as Spencer mentioned, you can put your info in a literal.

Another option is to use the System.Diagnostics.Debug.Assert() function, if you're debugging. The advantages to this are

  1. you can put these in your code and they will be compiled out of a release,
  2. you can place these in places where errors SHOULD NOT EVER occur, but will pop up to let you know when they do (only when you're debugging), and
  3. your code will pause functioning at the Assert line, so you know exactly what's going on where you put that Assert.

As with anything, don't go overboard and put them everywhere, but I've found it to be a very useful debugging tool.

Upvotes: 0

Max Schmeling
Max Schmeling

Reputation: 12509

The only way to get similar behavior (loosely similar) is to put a label within an update panel that is getting updated on that partial postback, and set its value to something (which would only take the one line of code to set it)... the rest of the page simply isn't getting updated, so there's nothing you can do.

Why do you want this functionality anyways?

Upvotes: 0

Spencer Ruport
Spencer Ruport

Reputation: 35117

You could just put a literal control inside your update panel and have the same effect using:

myLiteral.Text += "Some more text!";

Upvotes: 2

Related Questions