Page_load called twice on button click

Discussion in 'ASP .Net' started by Imran Aziz, Sep 3, 2005.

  1. Imran Aziz

    Imran Aziz Guest

    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.
    Imran Aziz, Sep 3, 2005
    #1
    1. Advertising

  2. Imran Aziz

    Imran Aziz Guest

    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" <> wrote in message
    news:%...
    > 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.
    >
    Imran Aziz, Sep 3, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:%...
    > 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.
    >
    S. Justin Gengo, Sep 3, 2005
    #3
  4. 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
    "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    message news:...
    > 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" <> wrote in message
    > news:%...
    >> 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.
    >>

    >
    >
    S. Justin Gengo, Sep 3, 2005
    #4
  5. Imran Aziz

    jasonkester Guest

    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/
    jasonkester, Sep 5, 2005
    #5
  6. Imran Aziz

    Imran Aziz Guest

    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" <> wrote in message
    news:%...
    > 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.
    >
    Imran Aziz, Sep 5, 2005
    #6
  7. Imran Aziz

    Guest

    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
    , Sep 6, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bminder
    Replies:
    1
    Views:
    2,150
    nfedin
    Feb 23, 2004
  2. Matt Hawley

    button's click handler gets called twice

    Matt Hawley, Apr 7, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    12,192
    Nikhil Patel
    Apr 7, 2004
  3. mike parr
    Replies:
    1
    Views:
    455
    Marina
    May 20, 2004
  4. z. f.
    Replies:
    0
    Views:
    546
    z. f.
    Oct 19, 2004
  5. Samy
    Replies:
    2
    Views:
    1,336
Loading...

Share This Page