1

I know such questions are asked already on SO, and i also went through most of them but no one satisfied me, so i am posting mine one. Attached is my code. actually i am building a jquery gallery from code from databale. see the code:

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DisplayHotProducts();
            }
        }



        private void DisplayHotProducts()
        {
            var dt = ProductData.GetAllHotProducsts();
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" <div id='spotlight_products' class='widget_box center_box'>");
                sb.AppendLine("<div class='wintitle'>");
                sb.AppendLine(" <div class='inner_wintitle'>");
                sb.AppendLine("  Hot Products</div></div>");
                sb.AppendLine("<div class='winbody'>");
                sb.AppendLine("<div class='mall_focus' id='mall_focus'>");
                sb.AppendLine("<a id='bp_carousel_prev'>prev</a>");
                sb.AppendLine("<a id='bp_carousel_next'>next</a>");
                sb.AppendLine("<div id='bp_carousel'>");
                sb.AppendLine(" <div class='carousel_list'>");


                for (int i = 0; i < dt.Rows.Count; i=i+5)
                {

                    sb.AppendLine(" <div class='product_list'>");
                    sb.AppendLine("<div class='bestproduct_wrapper'> <div class='bestproduct'>");
                    sb.AppendLine("   <div class='icon_best'></div>");
                    sb.AppendLine("<ul>");
                    sb.AppendLine(" <li class='best_Img'><span></span><a href='#' target='_blank'> ");
                    sb.AppendLine(string.Format(" <img src='{0}'  width='200' height='200'  /></a></li>", dt.Rows[i]["ImagePath"]));
                    sb.AppendLine("</ul>");
                    sb.AppendLine(" <p class='product_name'>");
                    sb.AppendLine(string.Format("<a href='Products/{0}'   target='_blank'>{1}</a></p>", dt.Rows[i]["Id"], dt.Rows[i]["Name"]));
                    sb.AppendLine("</div></div>");

                    for (int j = i+1; j <= 4; j++)
                    {
                        sb.AppendLine("<div class='product_item'>");
                        sb.AppendLine("<ul><li class='product_Img'>");
                        sb.AppendLine(string.Format("<a href='Products/{0}'><img src='{1}'  width='70' height='70'/></a>", dt.Rows[j]["Id"], dt.Rows[j]["ImagePath"]));
                        sb.AppendLine("</li></ul>");
                        sb.AppendLine(string.Format("<p class='product_name'><a href='Products/{0}'>{1}</a></p>", dt.Rows[j]["Id"], dt.Rows[j]["Name"]));
                        sb.AppendLine("</div>");
                    }
                }

                sb.AppendLine(@" </div></div></div></div></div>");
                ltlHotProducts.Text = sb.ToString();
            }
        }
    }

My code behind has the above code only and the page load is firing twice.

Also this is code behind of a usercontrol, placed on a page where i have 3 more similar control performing similar functionalities that is , building jquery slideshows from code behind. But the above two user controls has some issues with the img tags as some of them are missing images. Also the page on which the user controls are placed has a master page. Now please suggest me the solution.

So the heirarchy is something like this:

Master Page (master) -> Content Page (aspx) -> Has->3 UserControls (ascx)

1 Answers1

4

I have typically seen two reasons for the PageLoad to fire twice - First, if you have an img tag that doesn't have a src attribute, IE and Chrome will both request the page again, which causes the event to fire again. Second, if the event is wired up more than once, it'll fire more than once; Typically I've seen this happen when the code behind includes the event wireup code (eg, this.PageLoad += ... PageLoad) and the page directive AutoEventWireup=True is included in the aspx file.

Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
  • If the problem is img tags without src attributes, then remove those img tags (or make sure they get a valid src attribute, eg, by adding a "missing image" image). If the problem is wiring up the event twice, remove one of them (eg, delete the `this.PageLoad += ..` line of code or change `AutoEventWireup` to false. – Chris Shaffer Dec 18 '11 at 22:32
  • my img tags are coming blank in some other user controls but they are on same page. Do they create such issues? –  Dec 19 '11 at 08:35
  • They can (there is some discussion of this issue here: http://stackoverflow.com/questions/2009092/page-loads-twice-in-google-chrome) - There is a relatively easy way to check if that is the issue; Just remove the img tags temporarily and see if the problem goes away. – Chris Shaffer Dec 19 '11 at 11:58