Solved

Control events in GridView PagerTemplate get fired twice

Posted on 2009-07-15
5
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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