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

发布于 2024-08-24 01:22:24 字数 7312 浏览 7 评论 0原文

有什么不一样

<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] 

what is the difference between

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

and

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

?

And is the attribute AutoPostBack="True" always required?

Iam asking because it seems my OnSelectedIndexChanged-event seem always to be called twice (EDIT: When I use SelectedIndexChanged, the handler isn't called at all).

Why could this be?

EDIT:
these at the stacktraces I get from the event which is fired twice. The latter seem to be called from some kind of timer. I never created a time, it must be some of the system.

   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    

This is when the event is called the second time:

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] 

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

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

发布评论

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

评论(3

隐诗 2024-08-31 01:22:24

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

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

我这么问是因为这似乎是我的
OnSelectedIndexChanged-事件似乎
总是被叫两次......为什么
这可能吗?

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

what is the difference between [asp:DropDownList 1] and [asp:DropDownList 2]?

SelectedIndexChanged is not a valid attribute for an asp:DropDownList tag, so ASP.NET won't do anything with it.

I am asking because it seems my
OnSelectedIndexChanged-event seem
always to be called twice ... Why
could this be?

Your stack trace suggests that the second event invocation is caused by a re-entrant thread. Are you running any asynchronous delegates from your page, using client callbacks, or using a third-party control?

泪冰清 2024-08-31 01:22:24

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

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

The AutoPostBack property means that selection of the control will cause a post. If it is not set to true, then you will need to do something else to submit the form (i.e. a button click)

The possiblity on OnSelectedIndexChanged event, is check to make sure that you don't have it adding the event handler twice. If you have it defined in the ASPX, then do not have it being added on the codebehind and vice versa.

深居我梦 2024-08-31 01:22:24

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

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

编辑:

另一件事,如果我没有记错的话,OnSomething 方法通常是调用 Something 事件的方法(Edit2,Asp.NET(html)中的标记语言似乎不是这种情况。OnSomething html 中的 ="" 似乎映射到 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

If I recall correctly the events that's named OnSomething are more for internal use (like if you create a child-class you'd override the OnSelectedIndexChanged). So I think it's good practice to use the SelectedIndexChange.

As for the AutoPostBack, this is to make shure that the page posts back to the server when an item is changed. If this is not set to true then you (the server) will never now that it changed before the user clicks some submit-button or does something else that causes and autopostback (on another controll).

EDIT:

One more thing, if I don't recall wrongly a OnSomething method normally is the one that calls the Something event (Edit2, this seems not to be the case with the markup-language in Asp.NET (the html). OnSomething="" in html maps to the Something event it seems, my bad). So if you'd write a class "Person" with a name that could be changed you'd do it like this:

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;
}

or something like that.

Microsofts example's probably better than mine though :-P: http://msdn.microsoft.com/en-us/library/5z57dxz2.aspx

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