CslaDataSource issues a double call to DataSource_SelectObject

CslaDataSource issues a double call to DataSource_SelectObject

Old forum URL: forums.lhotka.net/forums/t/1505.aspx


CLoris posted on Monday, October 16, 2006

I have been banging my head on this one for a while and would appreciate some input.  In a nutshell, I have a business object that I am binding to a CslaDataSource after a date is selected from a calendar control, I get my Business object and bind it to the data source.  I then implement a caching scheme like Rocky did in the project tracker.  Code below:

Protected Sub calEditDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim curForecast As Forecast
   
If SurfMSG.Library.Forecast.Exists(1, calEditDate.SelectedDate) = True Then
      curForecast = Forecast.GetEditableRoot(1, calEditDate.SelectedDate)
       SetForecast(curForecast)
      DetailsView1.DataBind()
      DetailsView1.DefaultMode = DetailsViewMode.Edit
   Else
      
curForecast = Forecast.NewEditableRoot(1, calEditDate.SelectedDate)
      SetForecast(curForecast)
      DetailsView1.DataBind()
      DetailsView1.DefaultMode = DetailsViewMode.Insert
   
End If
   
If DetailsView1.Visible = False Then
      
DetailsView1.Visible = True
   
End If
End Sub

Protected Sub ForecastDataSource_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles ForecastDataSource.SelectObject
   e.BusinessObject = GetForecast()
End Sub

Private Sub SetForecast(ByVal myForecast As Forecast)
   Session(
"currentObject") = myForecast
End Sub

Private Function GetForecast() As Forecast
   
Dim businessObject As Object = Session("currentObject")
   
Return CType(businessObject, Forecast)
End Function

Everything works as expected except that ForecastDataSource_SelectObject is fired twice.

Any thoughts?

RockfordLhotka replied on Monday, October 16, 2006

Someone emailed me with an issue along this line weeks ago. I was unable to replicate it in the debugger with my code - and I think it ended up being some issue with their code.

It may be that you have multiple controls bound to that same data control - and each one would data bind independantly, triggering multiple events.

CLoris replied on Monday, October 16, 2006

Thank you for the response.  Can you give me any of the specifics of the other problem report.  Did it involve the DetailsView control?  Rebinding the data source?  I have very few controls on this page as it was part of some preliminary UI ideas.  Two Labels, Calendar Control and DataView.  One CslaDataSource.  Going to try and dig deeper.

RockfordLhotka replied on Tuesday, October 17, 2006

It has been a few weeks since this came up, and I don't recall the specifics, no, sorry.

I strongly suggest running your code in the debugger, with a breakpoint in your event handler. Then you can examine the stack trace to see what triggered the data binding process. It is most likely the case that you have an explicit DataBind() call in your code, in a case where ASP.NET is also automatically making the call on your behalf (like in Page_Load).

CLoris replied on Tuesday, October 17, 2006

I will do that.  A couple of interesting observations:

  1. There is a difference in behvaior if I add my SelectObject eventhandler for the CslaDataSource property window in the Form Designer versus when I simply add a Handles statement to the event handler in code.  When doing the later, the SelectObject event only fires once.  So - I go to the malfuncitoning form in form designer and remove the event handler from the CslaDatSource Event - SelectObject.  I go into the code behind file and add: Handles ForeastDataSource.SelectObject to Protected Sub ForecastDataSource_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs).  And then it works fine.
  2. Drilling down to the invocationList on the SelectObject event, I see two items registered for the event and they appear to be identical.

Is there an easy way to see what is being generated in the partial class for an ASPX page?  I thought this might be insightful.

CLoris replied on Thursday, November 16, 2006

Okay.  I figured out why this is happening.  In my CslaDataSource property sheet (In ASP.Net VB) I was double clicking on the SelectObject event to create the event handler:

ForecastDataSource_SelectObject

I noticed that the event handler was registered in two places/ways:

1. In the ASP CslaDataSource Control: OnSelectObject="ForecastDataSource_SelectObject"

2. In the codebehind I had a: Handles ForecastDataSource.SelectObject on the ForecastDataSource_SelectObject method

A few weeks ago when I was struggling with this, I swear that I could duplicate it.  I could not duplictae this double registering this morning.

So in a nutshell - double firing event handlers in ASP.Net VB - Look for both a Handles clause on the event handler and look for an event handler registration in the ASP control in the ASPX file.

RockfordLhotka replied on Thursday, November 16, 2006

Interesting!
 
I have a guess - there's an attribute on the page (aspx) that indicates whether automatic event hookups should occur. It is typically set one way in VB and the other in C# due to the lack of WithEvents in C#. Is it possible that this attribute got set backwards in your page? That could have triggered the C# style event hookup, and of course VS knows you are in VB and did the "right" thing for VB too?
 
Rocky
 

Copyright (c) Marimer LLC