2010-03-09 88 views
0

是什么问题有关asp.net事件处理

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

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

之间的区别?

而且AutoPostBack =“True”属性始终需要吗?

Iam问,因为它似乎我的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] 
+0

我对第二个例子的工作感到非常惊讶,因为Intellisense只给了我第一个例子。 – Greg 2010-03-09 15:21:47

+0

我现在发现第二个根本不工作。没有错误,但也没有事件处理程序调用 – codymanix 2010-03-09 15:35:42

回答

3

之间有什么区别[ASP:DropDownList的1]和[ASP:DropDownList的2]?

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

我问,因为它似乎我 OnSelectedIndexChanged事件似乎总是 被称为两次......为什么 会这样?

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

+0

Iam使用微软的ajax库。我不知道我怎么可能意外地包含异步委托或客户端回调而不知道它。 – codymanix 2010-03-09 15:39:08

+0

谢谢,现在我找到了它,因为你让我走上了正确的轨道。 Iam使用导致问题的AJAX ListSearchExtender。现在的问题是如何解决这个问题.. – codymanix 2010-03-09 15:51:15

+1

Hooray!你确定有什么需要解决的吗? 'ListSearchExtender'改变了下拉选择:当它这样做时,调用'OnSelectedIndexChanged'是否合适? – 2010-03-09 15:56:35

1

AutoPostBack属性意味着控制的选择将导致的柱。如果它没有设置为true,那么你将需要做其他事情来提交表单(即点击按钮)

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

+0

是的我确信我添加的只是在我的aspx文件 – codymanix 2010-03-09 15:14:15

+0

我以前发生过几次这种事。一定不要在ASPX和Codebehind中设置,否则它可能会搞砸页面...并尝试将其调用两次。你真的只需要一个或另一个(我更喜欢后面的代码)。 – Rob 2010-03-09 15:14:42

+0

我编辑了我的问题。我发现某种timercallback导致了这种情况,但它不是来自我的。 – codymanix 2010-03-09 15:25:17

0

如果我正确地记得名为OnSomething的事件更多用于内部使用(就像创建子类一样,您将覆盖OnSelectedIndexChanged)。所以我认为使用SelectedIndexChange是个好习惯。

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

编辑:

还有一两件事,如果我不记得一个错误的方法OnSomething通常是调用事件东西(EDIT2的一个,这似乎不是在ASP中标记语言的情况下.NET(html)。在html中的OnSomething =“”映射到Something事件似乎,我的坏)。所以如果你写一个名字可以改变的类“人”,你会这样做:

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

+0

我试过没有“On”,只是用了SelectedIndexChanged,但是这个处理程序根本没有被调用。此外,你给的页面是关于一般的.net事件,而不是他们如何使用asp.net – codymanix 2010-03-09 15:34:14

+0

你实际上是完全正确的。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedindexchanged.aspx (用一点googleing可以做些什么)。我忘记了Asp.net的标记语言(换句话说,HTML),他们在所有内容前加上了“On”。所以它也被称为OnClick ...我的错误。虽然它听起来很奇怪,但它被解雇了两次。对不起,帮不了你。 – Alxandr 2010-03-09 15:56:51