GridView bound by SqlDataSource, RowDataBound event gets called twice

I had a grid view with the DataSourceId property is set to a SqlDataSource control. Also it have a RowDataBound event bounded to have custom data formatting. Then I noticed that the RowDataBound is getting fired twice, as if it binding data twice.

The reason for that behavior I figured out after some hours of debugging. The reason was I have some code that set the column headers which assign language specific header titles on Page_Load() event. When you alter the column specific properties after data binding through a Data Source control, it always tries to get the data set again from the source. But this happen only when there is a change in the newly assigned data.

Because I had some pages doing the same thing, but they were not having this issue. Then I realized that if the newly assigned data differs from what exists in the GridView, only then it tries to call for another data fetch.

This won’t happen if you had created the data set separately, and assign it into the DataSource property.

 

Hope I have saved someones time.

Advertisements

ondblclick event and onclick event in HTML controls

As you are aware of the mouse click events, it actually triggers multiple events in the process of a one single click.

Some explanation can be taken from here.

http://msdn.microsoft.com/en-us/library/ie/ms536921%28v=vs.85%29.aspx

  1. onmousedown
  2. onmouseup
  3. onclick – successful consequtive onmousedown and onmouseup event on the same control will trigger an onclick event
  4. ondblclick – two event cycles of onclick event has to trigger this event

If you find a situation where you have to use the CLICK event and DOUBLE CLICK event on the same control. Then you will see onclick event get fired twice and then afterward double click event is fired. Most of the time people demand such functionality on data grids or tables for Row Level (aka. <tr> tags)  event handling/behaviors.

Jquery site also explain that it is inadvisable to bind both handlers but world demands much more than limitations.

http://api.jquery.com/dblclick/

But if you wanted to fire only the double click event but not the click events when a double click is occurred, well… that’s not doable. But there are ways that you can tackle with it. That is using timing events in javascript.

Declare a global variable to assign the reference of the setTimeout() function. So that you can clear it later. I name the variable as “alreadyclickedTimeout”

$("#ctrlid").click(function(){
   if (alreadyclickedTimeout) clearTimeout(alreadyclickedTimeout);
   alreadyclickedTimeout = setTimeout(function(){ 
      clearTimeout(alreadyclickedTimeout);
      //write what you want to do on click event
   }, 300); //300ms is sufficient to delay the next click event before dblclick event is identified, but this is system/user dependent value
}).dblclick(function(){
   //Now first and foremost what you have to do is to stop the click event before it get fired
   clearTimeout(alreadyclickedTimeout);
   //Write what you want to do on dblclick event
});

Above is a pretty simple code to handle the issue, but remember that it might fail if the user have assign a larger value to the double click event interval. More information on how to change that value can be found here http://en.wikipedia.org/wiki/Double-click

Because 900 mili-seconds value is the maximum in windows OS (it might vary in other OSes), if user’s actual speed is 400ms for the dbl click and system setting is 900ms, by the time of second click is occurred, the first click event’s function will be executed already. Afterward the dblclick event will get fired.

I have written a complete solution to handle  this situation. The sample code is shared here.

I have considered 900ms as the click event trigger time.  If you want to consider a more worst case scenario in some other OS, you can change 900 to a slightly bigger value.

Note: Don’t increase more than 1000ms, you don’t want your users to feel a doubtfulness in your system whether it works or not.

Also I think 900ms won’t be a problem, because you are planning to use single click and double click on the same control, of course the behaviors on that control is going to be complex and users are aware of it and I expect them to be bit more advanced users.

Note: Don’t give these type of features in public sites, it confuses the users, rather better to use a row level button or anchor element. But for corporate applications, such requirements are fine.

Happy coding!