Solved

Control events in GridView PagerTemplate get fired twice

Posted on 2009-07-15
5
658 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
javascript call 6 36
c# regex: extract & replace text between braces 6 28
Achieve json result 2 32
Adjust the position 3 12
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now