在 ASP.NET C# 中从 EventHandler 修改页面的正确方法是什么?
What is the correct way to modify Page from an EventHandler in ASP.NET C#?
举个例子:
我有一个由 Test.aspx 和 Test.aspx.cs.
组成的 Web 表单
我的 Page_Load 函数将事件处理程序 "EventHappensCompleted" 绑定到对象 "myThing" 发生的事件,同时对网页进行一些写入:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load");
try
{
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
我的 "EventHappensCompleted" 函数如下所示:
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
Response.Write("EventHappensCompleted");
}
当我第一次加载页面时,我会看到 "Page_Load" 写入页面。当第一个事件发生时,我会看到 "EventHappensCompleted" 写入页面,但之后的任何事件都不会写入页面。
我认为此行为与后续事件处理程序在不同线程上并且无法访问正确的上下文以写入页面有关,但我不确定。
如何修改此代码,以便事件处理程序每次都写入页面?
编辑 1:我应该明确指出,后续事件实际上正在发生。问题不在于事件实际上没有发生。
编辑 2:这是完整的 Test.aspx.cs:
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load");
try
{
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
Response.Write("EventHappensCompleted");
}
}
您的事件正在触发并且响应正在写入页面。之前的响应将不存在,因此看起来事件尚未触发。
这是因为在使用网络表单时,整个页面都会在响应中重新创建。所以你之前的回复不存在了。值得一读 ViewState in webforms,如果你想解决这个问题,你可以使用 AJAX 或添加到页面的客户端事件而不重绘整个 html 内容。
要解决这个问题,您可以实现计数或类似的东西...
protected void Page_Init(object sender, EventArgs e)
{
try
{
if (ViewState["counter"] != null)
{
int counter = Convert.ToInt32(ViewState["counter"]);
ViewState["counter"] = counter++;
}
else
{
int count = 1;
ViewState["counter"] = count;
}
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
而事件..
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
if (ViewState["counter"] != null)
{
var count = Convert.ToInt32(ViewState["counter"]);
for (int i = 0; i < count; i++)
{
Response.Write("EventHappensCompleted");
}
}
}
举个例子:
我有一个由 Test.aspx 和 Test.aspx.cs.
组成的 Web 表单我的 Page_Load 函数将事件处理程序 "EventHappensCompleted" 绑定到对象 "myThing" 发生的事件,同时对网页进行一些写入:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load");
try
{
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
我的 "EventHappensCompleted" 函数如下所示:
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
Response.Write("EventHappensCompleted");
}
当我第一次加载页面时,我会看到 "Page_Load" 写入页面。当第一个事件发生时,我会看到 "EventHappensCompleted" 写入页面,但之后的任何事件都不会写入页面。
我认为此行为与后续事件处理程序在不同线程上并且无法访问正确的上下文以写入页面有关,但我不确定。
如何修改此代码,以便事件处理程序每次都写入页面?
编辑 1:我应该明确指出,后续事件实际上正在发生。问题不在于事件实际上没有发生。
编辑 2:这是完整的 Test.aspx.cs:
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load");
try
{
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
Response.Write("EventHappensCompleted");
}
}
您的事件正在触发并且响应正在写入页面。之前的响应将不存在,因此看起来事件尚未触发。
这是因为在使用网络表单时,整个页面都会在响应中重新创建。所以你之前的回复不存在了。值得一读 ViewState in webforms,如果你想解决这个问题,你可以使用 AJAX 或添加到页面的客户端事件而不重绘整个 html 内容。
要解决这个问题,您可以实现计数或类似的东西...
protected void Page_Init(object sender, EventArgs e)
{
try
{
if (ViewState["counter"] != null)
{
int counter = Convert.ToInt32(ViewState["counter"]);
ViewState["counter"] = counter++;
}
else
{
int count = 1;
ViewState["counter"] = count;
}
var myThing = new MyThing();
myThing.EventHappens += EventHappensCompleted;
}
catch (Exception err)
{
Response.Write("ERROR");
}
}
而事件..
protected void EventHappensCompleted(object o, CustomEventArgs e)
{
if (ViewState["counter"] != null)
{
var count = Convert.ToInt32(ViewState["counter"]);
for (int i = 0; i < count; i++)
{
Response.Write("EventHappensCompleted");
}
}
}