Control events in GridView PagerTemplate get fired twice

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

LVL 1
mine_23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abelCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mine_23Author Commented:
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
mine_23Author Commented:
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
abelCommented:
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
mine_23Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.