View Full Version : SelectedIndexChanged on Gridview causing double postback
jleone
09-24-2007, 07:45 PM
Has anyone ever encountered a problem with a double postback? For some reason, my selectedindexchanged routine is running twice when selecting from the gridview. It goes through Page_Load then GridView1_SelectedIndexChanged then Pag_Load again and then GridView1_SelectedIndexChanged again, causing undesirable results based on things stored in objects? Any help would be appreciated. None of my code could the be the culprit (at least we don't think), but my supervisor and I are wondering if something else behind the scenes makes it do that.
jleone
09-25-2007, 07:19 PM
Well after some further searching on the web, it appears that it does this by design. The GridView itself causes a postback and then because there is an input type button being clicked, the onclick has a postback as well.
I worked around this by using a session variable. When I start the OnSelectedIndexChanged for the Gridview, I do a check for a session variable. If it's nothing, I do my code and then populate my session variable with something. When it comes through the second time, my code sees that the session variable is something, so I set the session variable back to nothing and then I redirect to the page I need to. Problem solved.
Barry Novak
10-07-2007, 08:21 PM
I had the same issue, and thanks to your post, I stopped looking for where I went wrong, and fixed it the same way you did (with a Session variable). I'm curious though...do you remember where you found out that it does this by design?
nikkiH
10-08-2007, 03:43 PM
It would happen if you had a button click to handle the change instead of having the drop down autopostback. Both the selected index changed and the onclick would fire.
jleone
10-08-2007, 06:43 PM
I had the same issue, and thanks to your post, I stopped looking for where I went wrong, and fixed it the same way you did (with a Session variable). I'm curious though...do you remember where you found out that it does this by design?
Various google searches returned info about this, which led me to use the session variable as the workaround.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.