【问题标题】:Question about asp.net event handling关于asp.net事件处理的问题
【发布时间】:2010-03-09 15:05:38
【问题描述】:

有什么区别

<asp:DropDownList ID="Combo" OnSelectedIndexChanged="Change" AutoPostBack="True" runat="server"/>

<asp:DropDownList ID="Combo" SelectedIndexChanged="Change" AutoPostBack="True" runat="server"/>

?

属性 AutoPostBack="True" 是否总是必需的?

我问是因为我的 OnSelectedIndexChanged 事件似乎总是被调用两次(编辑:当我使用 SelectedIndexChanged 时,根本不调用处理程序)。

为什么会这样?

编辑: 这些在我从触发两次的事件中获得的堆栈跟踪中。后者似乎是从某种计时器调用的。我从来没有创建过时间,一定是系统的一些。

   MyApp!MyNamespace.ListenControl.AuswahlGeändert(object sender = {System.Web.UI.WebControls.DropDownList}, System.EventArgs e = {System.EventArgs}) Zeile 44  C#
    System.Web.dll!System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(System.EventArgs e = {System.EventArgs}) + 0x70 Bytes   
    System.Web.dll!System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() + 0x87 Bytes  
    System.Web.dll!System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() + 0xb Bytes    
    System.Web.dll!System.Web.UI.Page.RaiseChangedEvents() + 0xa6 Bytes 
    System.Web.dll!System.Web.UI.Page.ProcessRequestMain(bool includeStagesBeforeAsyncPoint = true, bool includeStagesAfterAsyncPoint = true) + 0x5ce Bytes 
    System.Web.dll!System.Web.UI.Page.ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint = true) + 0x84 Bytes 
    System.Web.dll!System.Web.UI.Page.ProcessRequest() + 0x51 Bytes 
    System.Web.dll!System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext context) + 0x16 Bytes   
    System.Web.dll!System.Web.UI.Page.ProcessRequest(System.Web.HttpContext context) + 0x32 Bytes   
    App_Web_bm-nkhnz.dll!ASP.kfzpage_aspx.ProcessRequest(System.Web.HttpContext context = {System.Web.HttpContext}) + 0x26 Bytes    C#
    System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0xb6 Bytes 
    System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.CallHandlerExecutionStep}, ref bool completedSynchronously = true) + 0x4c Bytes  
    System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x133 Bytes  

    System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0x7c Bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest wr = {Microsoft.VisualStudio.WebHost.Request}) + 0x17c Bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest wr) + 0x63 Bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest wr) + 0x47 Bytes  
    WebDev.WebHost.dll!Microsoft.VisualStudio.WebHost.Request.Process() + 0x165 Bytes   
    WebDev.WebHost.dll!Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Microsoft.VisualStudio.WebHost.Connection conn = {System.Runtime.Remoting.Proxies.__TransparentProxy}) + 0x6c Bytes   
    [Anwendungsdomänenübergang] 
    WebDev.WebHost.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(object acceptedSocket) + 0x7d Bytes 
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(object state) + 0x2f Bytes   
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f Bytes    
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(System.Threading._ThreadPoolWaitCallback tpWaitCallBack) + 0x53 Bytes 
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(object state) + 0x59 Bytes    

这是第二次调用事件的时候:

MyApp!MyNamespace.ListenControl.AuswahlGeändert(object sender = {System.Web.UI.WebControls.DropDownList}, System.EventArgs e = {System.EventArgs}) Zeile 44 C#
        System.Web.dll!System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(System.EventArgs e = {System.EventArgs}) + 0x70 Bytes   
        System.Web.dll!System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() + 0x87 Bytes  
        System.Web.dll!System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() + 0xb Bytes    
        System.Web.dll!System.Web.UI.Page.RaiseChangedEvents() + 0xa6 Bytes 
        System.Web.dll!System.Web.UI.Page.ProcessRequestMain(bool includeStagesBeforeAsyncPoint = true, bool includeStagesAfterAsyncPoint = true) + 0x5ce Bytes 
        System.Web.dll!System.Web.UI.Page.ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint = true) + 0x84 Bytes 
        System.Web.dll!System.Web.UI.Page.ProcessRequest() + 0x51 Bytes 
        System.Web.dll!System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext context) + 0x16 Bytes   
        System.Web.dll!System.Web.UI.Page.ProcessRequest(System.Web.HttpContext context) + 0x32 Bytes   
        App_Web_bm-nkhnz.dll!ASP.kfzpage_aspx.ProcessRequest(System.Web.HttpContext context = {System.Web.HttpContext}) + 0x26 Bytes    C#
        System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0xb6 Bytes 
        System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.CallHandlerExecutionStep}, ref bool completedSynchronously = true) + 0x4c Bytes  
        System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x133 Bytes  

        System.Web.dll!System.Web.HttpApplication.ResumeStepsFromThreadPoolThread(System.Exception error) + 0x25 Bytes  
        System.Web.dll!System.Web.HttpApplication.AsyncEventExecutionStep.ResumeStepsWithAssert(System.Exception error) + 0x28 Bytes    
        System.Web.dll!System.Web.HttpApplication.AsyncEventExecutionStep.OnAsyncEventCompletion(System.IAsyncResult ar) + 0x84 Bytes   
        System.Web.dll!System.Web.HttpAsyncResult.Complete(bool synchronous, object result, System.Exception error, System.Web.RequestNotificationStatus status) + 0x3e Bytes   
        System.Web.dll!System.Web.SessionState.SessionStateModule.PollLockedSessionCallback(object state) + 0x169 Bytes 
        mscorlib.dll!System.Threading._TimerCallback.TimerCallback_Context(object state) + 0x2f Bytes   
        mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x51 Bytes 
        [Übergang von Systemeigen zu Verwaltet] 
        [Übergang von Verwaltet zu Systemeigen] 
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x67 Bytes    
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x45 Bytes    
        mscorlib.dll!System.Threading._TimerCallback.PerformTimerCallback(object state) + 0x6b Bytes    
        [Anwendungsdomänenübergang] 

【问题讨论】:

  • 我真的很惊讶第二个例子能正常工作,因为 Intellisense 只给了我第一个例子。
  • 我现在发现第二个根本不起作用。没有错误,但也没有调用事件处理程序

标签: asp.net events postback web-controls


【解决方案1】:

[asp:DropDownList 1]和[asp:DropDownList 2]有什么区别?

SelectedIndexChanged 不是 asp:DropDownList 标记的有效属性,因此 ASP.NET 不会对它做任何事情。

我问是因为它似乎是我的 OnSelectedIndexChanged 事件似乎 总是被调用两次......为什么 会是这样吗?

您的堆栈跟踪表明第二个事件调用是由可重入线程引起的。您是否从页面运行任何异步委托、使用客户端回调或使用第三方控件?

【讨论】:

  • 我正在使用 microsoft ajax 库。我不知道我怎么可能在不知情的情况下意外包含异步委托或客户端回调。
  • 谢谢,现在我找到了,因为你让我走上了正轨。我正在使用导致问题的 AJAX ListSearchExtender。现在的问题是如何解决这个问题..
  • 万岁!你确定有什么需要修复的吗? ListSearchExtender 更改下拉选择:这样做时调用 OnSelectedIndexChanged 不合适吗?
  • 但是我不需要两次调用事件处理程序,这只会造成不必要的流量。
【解决方案2】:

AutoPostBack 属性意味着选择控件将导致发布。如果未设置为 true,那么您将需要执行其他操作来提交表单(即单击按钮)

OnSelectedIndexChanged 事件的可能性是检查以确保您没有两次添加事件处理程序。如果您在 ASPX 中定义了它,则不要在代码隐藏中添加它,反之亦然。

【讨论】:

  • 我以前也遇到过几次这种情况。确保不要在 ASPX 和 Codebehind 中设置,否则它可能会搞砸页面......并尝试调用它两次。你真的只需要一个或另一个(我更喜欢后面的代码)。
  • 我编辑了我的问题。我发现某种 timercallback 导致了这种情况,但它不是来自我。
【解决方案3】:

如果我没记错的话,名为 OnSomething 的事件更多的是供内部使用(例如,如果您创建一个子类,您将覆盖 OnSelectedIndexChanged)。所以我认为使用 SelectedIndexChange 是个好习惯。

对于 AutoPostBack,这是为了确保页面在项目更改时回发到服务器。如果未将其设置为 true,那么您(服务器)将永远不会在用户单击某些提交按钮或执行其他导致自动回发(在另一个控件上)之前更改它。

编辑:

还有一件事,如果我没有记错的话,OnSomething 方法通常是调用Something 事件的方法(Edit2,Asp.NET 中的标记语言(html)似乎不是这种情况。 html 中的 OnSomething="" 映射到Something 事件似乎,我的错)。因此,如果您要编写一个名称可以更改的“Person”类,您可以这样做:

public class Person
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; OnNameChanged(new EventArgs()); }
    }

    protected OnNameChanged(EventArgs args)
    {
        if(NameChanged != null)
            NameChanged(this, args);
    }

    public event EventHandler NameChanged;
}

或类似的东西。

微软的例子可能比我的要好 :-P: http://msdn.microsoft.com/en-us/library/5z57dxz2.aspx

【讨论】:

  • 我在没有“On”的情况下尝试了它,只是使用了 SelectedIndexChanged,但是根本没有调用处理程序。此外,您提供的页面一般是关于 .net 事件,而不是它们如何与 asp.net 一起使用
  • 其实你说的很对。 msdn.microsoft.com/en-us/library/…(谷歌搜索可以做什么)。我忘记了在 Asp.net 的标记语言(换句话说就是 html)中,他们在所有内容前面都添加了“On”。所以它也被称为 OnClick ......我的错误。虽然它被发射了两次仍然听起来很奇怪.. 抱歉,帮不了你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多