Page_load called twice on button click

I

Imran Aziz

Hello All,
I have a search text and button that post data and my button handler
filters the repeater control. However when the button is clicked the first
time. The page_load event is being called twice, once with postback true,
and second time without postback. This only happens for this button. How can
I sort out this issue, any clues what to look for to get this sorted please.

<p>Search Bookmarks: <asp:TextBox ID="txtSearch" runat="server"
class="texta" OnTextChanged="btnSearch_Click"
AutoPostBack="true"></asp:TextBox><asp:Button ID="btnSearch" runat="server"
class="button" Text="go" OnClick="btnSearch_Click" UseSubmitBehavior="true"
/></p>

Imran.
 
I

Imran Aziz

One more thing I must add is that it only happens for the first time, that
the page_load event is called twice on subsequent requests this does not
happen, and the page works fine.

Any clues please, I am quite stuck now, tried a number of things, but cannot
seem to understand what's going on.

Imran.
 
S

S. Justin Gengo

Imran,

I saw it right away. You've got your text box set to autopost back on text
changed. So the moment you leave the text box your page posts back and runs
the page load then again for the button click. I believe that your
btnSearch_Click routine is probably being called twice also.

Assuming that you are using the ontextchanged routine to handle if the user
hits the enter key while in the text box there are two ways you can stop
this behaviour:

One would be to stop the button click itself altogether with a javascript
such as: btnSearch.Attributes.Add("onclick", "javascript: return false;")
but doing so will stop a user from re-running a search by clicking the
button a second time. They would only be able to search when entering new
text in the search text box.

Another way (which is the method I prefer) is to remove autopost back from
the text box and make hitting the enter key while in the text box click the
search button. Do that like this: btnSearch.Attributes.Add("onkeydown",
"javascript:(if((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13)){document.getElementById('" & btnSearch.ClientID &
"').click();return false;}else return true;")

If you'd like I have created a free Javascript component that has many
useful scripts in it. You simply drag it onto the page you want to use it on
(in the designer) and then in the code-behind call any of its methods and
they automatically hook up scripts to the page controls to do things like
click a button when the enter key is hit while in a text box, pop-up a
centered window, add a javascript confirmation window on a button click,
scroll a control into view (replacing smart navigation), stopping a user
from clicking a submit button more than once, etc. You may download the
component as a .NET project with all source code for free from my web site
http://www.aboutfortunte.com just click on the "Code Library" link at the
top of the page and then click the "Javascript" button on the left side of
the page. There is also a full help file for all the components I've
developed and shared.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

Imran,

I just noticed that the code I posted to you hooks up the enter key check to
the search button when I should have shown it hooked up to the text box.
Sorry about that.

Here's the proper code:

txtSearch.Attributes.Add("onkeydown", "javascript:(if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById('" & btnSearch.ClientID & "').click();return
false;}else return true;")



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
I

Imran Aziz

Very strange, but I seem to have found out what the issue was. This page
that I call is a Server.Transfer from a default page. And when its be
transferred control from another page, and one does a post back , it seems
to be doing it twice, but when I call this page up by itself it works
perfectly fine. So I guess I will use Response.Redirect to transfer control
to this page directly rather then use Server.Transfer to transfer control.
Imran.
 
M

michaelcoleman72

I have used Server.Transer before, but you have these hidden jewels
from time to time. ASP.Net plays better with Response.Redirect.

Regards
Coleman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top