the page load event execute twice

sabry sabry 1 Reputation point
2022-07-06T11:22:55.333+00:00

if have this link in word file when click it open the page offers.aspx
http://localhost:54818/offerslinks/offers.aspx?Offerid=80

and in the page load I wrote this code

   protected void Page_Load(object sender, EventArgs e)  
    {  
    if (!IsPostBack)  
    {  
        // GET THE ID OF THE OFFER   
        string id = Request.QueryString["Offerid"];  
        // GET THE LINK AND THE LINK COUNT   
        
        if (id != null)  
        {  
            string linkName = "";  
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
            using (SqlConnection con = new SqlConnection(cs))  
            {  
                SqlCommand cmd = new SqlCommand("SpOfferbyId", con);  
                cmd.CommandType = CommandType.StoredProcedure;  
                SqlParameter paramid = new SqlParameter("@ID", Convert.ToInt32(id));  
                cmd.Parameters.Add(paramid);  
                con.Open();  
                SqlDataReader rdr = cmd.ExecuteReader();  
                while (rdr.Read())  
                {  
                    linkName = rdr["Link"].ToString();  
                     
                }  

                Response.Redirect(linkName,false);  
                id = string.Empty;  
            }  

        }//end if   

          
    }  

when I made debugging I found the query string read the parameter and execute the code 1- execute the page-load of page content 2- execute the page-load of the master page 3- ec=xecute the page-load of the content page again

ps: the page is empty

Developer technologies .NET Other
Developer technologies ASP.NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-06T15:29:22.84+00:00

    Use the browsers network trace to see if it calls twice. An empty img or style href will do this.


  2. Albert Kallal 5,586 Reputation points
    2022-07-19T21:19:28.157+00:00

    You might perhaps want to post the button (markup) used.

    I have several times over the years wasted a whole after noon.

    take this simple button:

    222502-image.png

    And, you get this:

    222503-image.png

    Now, in MOST cases, I use a asp.net button), but a plain jane button I often use since I can then with greater ease include a bootstrap glyphicon as per above.

    So, any readers see a problem with above?

    No, you don't. Now, I not been able to determine WHEN, but we OFTEN will find the above button when clicked will fire 2 times. (and that means page load fires two times, and then the event code for the button ALSO fires two times!!!).

    Solution:

    Add type button to the markup:

    222453-image.png

    So, the above is probably NOT your issue - but if you using a button as opposed to a asp.net button, then do check the above.

    and if you using a standard asp.net button? Then do not have BOTH a onclick="my event stub", and a handles event for the button. (use one, or the other - if you use both, then you again get the button code to trigger two times).

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.