473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Page_load called twice on button click

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.
Nov 19 '05 #1
3 8695
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" <im***@tb2.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

Nov 19 '05 #2
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
"Imran Aziz" <im***@tb2.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

Nov 19 '05 #3
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

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: thejackofall | last post by:
Hi. I have a button called btnSelect on a web form. There is a handler for it. However, the Page_Load() gets called before the handler gets called. I want to do the following. In the...
0
by: Itai | last post by:
Background: I have four Web Form pages with respective C# code behind files, all in the same project: localhost/vpath1 Page1.aspx Page2.aspx
2
by: Mark Rae | last post by:
I've inherited an ASP.NET app and have been asked to fix the following problem with a page which is used to display and/or delete records from a database. When the page loads, it displays a list of...
6
by: Dot net work | last post by:
I've read quite a few threads on these groups about this. When someone says the following: "My Page_Load gets called twice on a button click postback" The replies are: "Do you have...
1
by: puja | last post by:
hi all, I have this .aspx page for which the Page_load event occurs twice. I found out while debugging. After searching google, I tried checking with Page.Ispostback method and also had...
11
by: fiefie.niles | last post by:
I am using ASP.NET 2005 and I have a simple form. Page_Load calls a sub mySub that does not do anything (for testing purposes). But, Page_Load gets called twice. On every single ASPX page in my...
4
by: David C | last post by:
I spent the last four hours trying to figure out why Page_Load would execute twice. Even stranger was that everything within if (! IsPostBack){....} executed twice as well. There is no rhyme or...
5
by: Jason Huang | last post by:
Hi, In my ASPNet 2.0 C# web application Form1.aspx, there are 1 TextBox control txtBox1, 1 Button control Btn1, and 1 GridView control GV1. I found whenever I click the Button control Btn1, then...
4
by: Paul | last post by:
I have a problem with asp.net 2.0 I've never seen before. On loading the page it loads twice and IsPostback is false both times. This happens if I use: Response.Redirect("somepage.aspx"); I've...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.