473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 8685
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.