Solved

Control events in GridView PagerTemplate get fired twice

Posted on 2009-07-15
5
663 Views
Last Modified: 2012-06-22
Hi,
I am using a GridView with ObjectDataSource. In the GridView, I have a PagerTemplate with the following links:

The navigation links work great. However, If I put a Textbox control in the PagerTemplate, and attach a LOAD event to it,  this event will get fired twice (after clicking any navigation link from above to cause postback).
If I move  the same control outside the GridView, everything works fine.

Why does that occur?

<asp:LinkButton ID="LinkButtonFirst" ToolTip="Go to First page"   runat="server" Text="First" CommandName="Page" CommandArgument="First"/>
 
<asp:LinkButton ID="LinkButtonPrevious" ToolTip="Go to Previous page"      runat="server" Text="Previous" CommandName="Page" CommandArgument="Prev" />                    
 
<asp:LinkButton ID="LinkButtonNext" ToolTip="Go to Next page"    runat="server" Text="Next" CommandName="Page" CommandArgument="Next" />
 
<asp:LinkButton ID="LinkButtonLast" ToolTip="Go to Last page"   runat="server" Text="Last" CommandName="Page" CommandArgument="Last" />

Open in new window

0
Comment
Question by:mine_23
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
abel earned 100 total points
ID: 24931420
This behavior is "by design" and happens because of the way the GridView is implemented and renders (there are other controls that suffer the same problem in different situations, i.e. the selection_changed of a selection control). The preferred workaround goes as follows:

private bool _tbLoadHandled = true;
protected void TB_Load(object sender, EventArgs e)
{
   _tbLoadHandled = !_tbLoadHandled;   // this switches to true every second time
   if(_tbLoadHandled) 
       return;
   
   // your normal code here
}

Open in new window

0
 
LVL 1

Author Comment

by:mine_23
ID: 24932120
Yes, but this does not help much as sometimes this event fires only once, such as when clicking on an element other than navigation links!
Now to keep tracking with more variables is really frustrating and will mess up all my tidy code :(

I reported this to Microsoft Connect, but they said "It is a normal behavior" though I don't see any normality in this.
0
 
LVL 1

Author Comment

by:mine_23
ID: 24932138
When this event fires twice, I'll need to access my code in the 2nd event. But when it fires once, then I'll handle my code in this.
0
 
LVL 39

Assisted Solution

by:abel
abel earned 100 total points
ID: 24933608
The event should fire twice always for one object and never for another, depending on where and how it is used. But same situations give same behavior.

Yes, I had the same experience with reporting it to Microsoft many years ago. It is "by design" though like you I find it hard to see the proper "design" and helpfulness of this behavior.

If you need this a lot (I usually only need it for one or two controls and then I can live with the ugly code), you can consider writing an attribute, say OncePerRequestAttribute. You then create a method (possibly an extension method to UserControl and Page to make it easy to call them) which you simply call in the beginning of these methods that are called twice which then checks this attribute and if it is there takes care of the rest (remembering how often it was called etc).

If you want that bullet proof and such that it also works when you apply the attribute and the method-call to events that get fired only once, you should add a session variable of some sort in the Application_PostAcquireRequestState event (or similar), which you can use as a key to track whether it is the first or a subsequent call to a method.

And if you want even that a bit cleaner, you can create startup code in the Application_Start which augments the event handlers which have that attribute, but this is getting very advanced and requires either dynamic methods or IL injection.

-- Abel --
0
 
LVL 1

Author Comment

by:mine_23
ID: 24940342
Thanks!
I had to use HttpContext.Current.Items  to track this event and used Session to store textbox's value since the value is cleared on every postback :(, I ended up with messy code. I think I should have used a ListView instead. The weird thing is that the content is cleared even if I use EnableViewState=true

Anyway, many thanks!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 54
Ajax Panel Not Updating When Triggered By Timer 4 73
performance issue in asp web 5 33
Need to start a web service from Visual Studio 2015 Pro 2 24
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question