关于asp.net事件处理的问题

时间:2010-03-09 15:05:38

标签: asp.net events postback web-controls

之间有什么区别
<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] 

3 个答案:

答案 0 :(得分:3)

  

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

SelectedIndexChanged不是asp:DropDownList标记的有效属性,因此ASP.NET不会对其执行任何操作。

  

我在问,因为它似乎是我的   OnSelectedIndexChanged-event似乎   总是被叫两次......为什么   这可能是吗?

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

如果我没记错的话,名为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