Page load firing multiple times.

Having a unusual problem with an application I'm building, so what I'm looking to do is a user can view preview of their adverts they have created, click the advert which is a dynamic link and be brought to a new page which will show the advert in more detail. The more detail page is then runs a to check the query string, pass the parameters of the query sting to a method which pulls the correct details form the database. This part works perfectly, but when the object is returned the pages loads again, calls the same method 3 times, but with null parameters which in returns a null reference to an object that doesn't exist.

I've tried to refactor the code so when a page loads it won't retrieve form the database unless the search parameters are valid. When the page loads, components won't work then, for example the Image Carousel.

I've tried hard coding the parameters into the method, will work perfectly. will only load once.

I've created two new pages which do not inherit form a master page and used the exact same methods. works perfectly. I tried the method on a two different pages that are inherited form the master page and same problem.

DetailedPage.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BLL; using DataModels; namespace { public partial class DetailAdvert : System.Web.UI.Page { public Advert_User ReturnedUserAdvet = new Advert_User(); public BalAdvertManger BALMan = new BalAdvertManger(); protected void Page_Load(object sender, EventArgs e) { //Response.Cache.SetCacheability(HttpCacheability.NoCache); //Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1); //GetData(); //PopulatePage(); //int UserID = Convert.ToInt32(Request.QueryString["UiD"]); //int AdvertID = Convert.ToInt32(Request.QueryString["AdID"]); if (!Page.IsPostBack) { GetData(); } } public void GetData() { //int UserID = 41; //int AdvertID = 2; int UserID = Convert.ToInt16(Request.QueryString["UiD"]); int AdvertID = Convert.ToInt16(Request.QueryString["AdID"]); if (UserID != 0 || AdvertID != 0) { ReturnedUserAdvet = BALMan.ReturnDetailedAdvert(UserID, AdvertID); PopulatePage(); } //int UserID = Convert.ToInt32(Request.QueryString["UiD"]); //int AdvertID = Convert.ToInt32(Request.QueryString["AdID"]); //int UserID = 41; //int AdvertID = 2; //ReturnedUserAdvet = BALMan.ReturnDetailedAdvert(UserID,AdvertID); } public void PopulatePage() { } } } 


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeFile="DetailAdvert.aspx.cs" Inherits="" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <div class="col-lg-9"> <div class="container"> <div class="row"> <!--Advert Image --> <div class="container"> <div class="row"> <!-- Carosel goes here --> <div class="row carousel-holder"> <div class="col-xs-12 col-sm-12 col-md-12 col-lg-10"> <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div class="item active"> <img class="slide-image" id="Img1" runat="server" src="; alt=""/> </div> <div class="item"> <img class="slide-image" id="Img2" runat="server" src="; alt=""/> </div> <div class="item"> <img class="slide-image" id="Img3" runat="server" src="; alt=""/> </div> </div> <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> </div> </div> </div> </div> <!--User Details--> <br /> <div class="container"> <div class="row"> <!-- Descriptin and profile view --> <div class="col-xs-12 col-sm-6 col-md-6 col-lg-5"> <div class="well well-sm"> <div class="row"> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"> <img src="" runat="server" id="imgProfilePic" alt="" class="img-rounded img-responsive" /> </div> <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4"> <h4> <asp:Label ID="lblFristName" runat="server" Text=""></asp:Label> <asp:Label ID="lblSecondName" runat="server" Text=""></asp:Label> </h4> <small><cite title="Location"><asp:Label ID="lblLocation" runat="server" Text=""></asp:Label> <i class="glyphicon glyphicon-map-marker"> </i></cite></small> <p> <i title="Email" class="glyphicon glyphicon-envelope"></i> <asp:Label ID="lblEmail" runat="server" Text=""></asp:Label> <br /> <i title="Join Date" class="glyphicon glyphicon-user"></i> <asp:Label ID="lblJoinDate" runat="server" Text=""></asp:Label><br /> <i title="User Rating" class="glyphicon glyphicon-stats"> </i><asp:Label ID="lblUserRating" runat="server" Text=""></asp:Label><br /> <i title="Contact Number" class="glyphicon glyphicon-phone"> </i><asp:Label ID="lblContactNumber" runat="server" Text=""></asp:Label> </p> <!-- Split button --> <asp:Button ID="btnMessage" runat="server" Text="Message" CssClass="btn btn-success" /> </div> </div> </div> </div> <!-- Description / Product Details--> <div class="col-xs-12 col-sm-6 col-md-6 col-lg-5"> <div class="well well-sm"> <h4> Description: </h4> <p> <asp:Label ID="lblDesc" runat="server" Text=""></asp:Label> </p> </div> </div> </div> </div> <div class="container"> <div class="row"> <!-- Comment Section --> <div class="col-xs-12 col-sm-12 col-md-12 col-lg-10"> <div class="well well-sm"> <h4>Comments</h4> </div> </div> </div> </div> </div> </div> </div> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server"> </asp:Content> 

by Tiddleywanksofcum via /r/csharp

Leave a Reply