Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Page_load called twice on button click

Reply
Thread Tools

Page_load called twice on button click

 
 
Imran Aziz
Guest
Posts: n/a
 
      09-03-2005
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.


 
Reply With Quote
 
 
 
 
Imran Aziz
Guest
Posts: n/a
 
      09-03-2005
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.

"Imran Aziz" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-03-2005
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",
"javascriptif((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
"Imran Aziz" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-03-2005
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", "javascriptif((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
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:(E-Mail Removed)...
> 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", "javascriptif((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
> "Imran Aziz" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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.
>>

>
>



 
Reply With Quote
 
jasonkester
Guest
Posts: n/a
 
      09-05-2005
Make sure you don't have SmartNav in the mix anywhere. It likes to
post back a few extra times.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

 
Reply With Quote
 
Imran Aziz
Guest
Posts: n/a
 
      09-05-2005
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.

"Imran Aziz" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
michaelcoleman72@gmail.com
Guest
Posts: n/a
 
      09-06-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
in VB.NET Page_load of a base class called after the derived class Page_load ? z. f. ASP .Net 0 10-19-2004 12:01 PM
button click event being called twice on click mike parr ASP .Net 1 05-20-2004 04:02 PM
button's click handler gets called twice Matt Hawley ASP .Net 5 04-07-2004 02:01 PM
Why does page_load fire twice when inheriting from a common overridable Page_Load bminder ASP .Net 1 02-23-2004 08:54 PM



Advertisments