[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Control events in GridView PagerTemplate get fired twice

Posted on 2009-07-15
5
Medium Priority
?
683 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 400 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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