在更新面板中执行代码时类似 Response.Write 的行为

发布于 2024-08-02 18:54:50 字数 100 浏览 3 评论 0原文

有没有办法让 Response.Write 在 UpdatePanel 中工作,并且不会导致应用程序因明显原因而出错?或者,是否有一种方法无需使用超过一行 C# 代码即可获得类似的结果?

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゃ人海孤独症 2024-08-09 18:54:50

您可以在更新面板中放置一个文字控件,并使用以下命令获得相同的效果:

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

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

myLiteral.Text += "Some more text!";
清浅ˋ旧时光 2024-08-09 18:54:50

刚刚发现这可能对其他人有帮助。

您不能在更新面板内使用 Response.Write 或 Response.Redirect。

为了解决这个问题,你必须使用触发器。这用于从更新面板内部进行服务器访问

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

  ---Your code here

</ContentTemplate>

这里的 ControlID 是按钮或其他控件。例如,在按钮单击事件中,您可以在响应中写入一些文本。

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.

随梦而飞# 2024-08-09 18:54:50

获得类似行为(大致相似)的唯一方法是在更新面板中放置一个标签,该标签在该部分回发时进行更新,并将其值设置为某个值(这只需要一行代码来设置它)。 ..页面的其余部分根本没有更新,所以你无能为力。

为什么你想要这个功能呢?

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?

橘虞初梦 2024-08-09 18:54:50

Response.Write 不适用于 UpdatePanels,但正如 Spencer 提到的,您可以将信息放入文字中。

如果您正在调试,另一种选择是使用 System.Diagnostics.Debug.Assert() 函数。这样做的优点是

  1. 你可以将它们放入你的代码中,它们将从版本中编译出来,
  2. 你可以将它们放在不应该发生错误的地方,但会弹出来让你知道它们何时发生(仅当你正在调试),并且
  3. 您的代码将在 Assert 行暂停运行,因此您可以确切地知道放置该 Assert 的位置发生了什么。

与任何事情一样,不要太过分,也不要把它们到处乱放,但我发现它是一个非常有用的调试工具。

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.

茶色山野 2024-08-09 18:54:50

我使用的解决这个问题的一个简单方法是调用 JQuery 函数 html() 并调用 Response.Write()...例如,假设我想更新 UpdatePanel 中的一些 html 文本,我会这样做:

使用 Response.Write (),那就简单了:
Response.Write("[TextToBeAddedToHTML]");

但使用 Jquery 有点复杂,你必须将 Jquery 库包含到 Html 页面中:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文