title: Random background image on DIV
body: <p>I'm trying to get a randomly picked background image (from a selection of 4 images) to appear as the background image for a asp.net panel.</p>
  
  <p>The problem I have is that the code <strong>works</strong> when stepping through the code in debug mode. Once you run the code on the website without debugging, all the images are the same. Its almost as if the random number is not getting picked up quick enough.</p>
  
  <p>The usercontrol is inside of a datalist.</p>
  
  <p>The usercontrol is this:</p>
  
  <pre><code>&lt;asp:Panel ID="productPanel" CssClass="ProductItem" runat="server"&gt;
  
  &lt;div class="title" visible="false"&gt;
      &lt;asp:HyperLink ID="hlProduct" runat="server" /&gt;
  &lt;/div&gt;
  &lt;div class="picture"&gt;
      &lt;asp:HyperLink ID="hlImageLink" runat="server" /&gt;
  &lt;/div&gt;
  &lt;div class="description" visible="false"&gt;
      &lt;asp:Literal runat="server" ID="lShortDescription"&gt;&lt;/asp:Literal&gt;
  &lt;/div&gt;
  &lt;div class="addInfo" visible="false"&gt;
      &lt;div class="prices"&gt;
          &lt;asp:Label ID="lblOldPrice" runat="server" CssClass="oldproductPrice" /&gt;
          &lt;br /&gt;
          &lt;asp:Label ID="lblPrice" runat="server" CssClass="productPrice" /&gt;&lt;/div&gt;
      &lt;div class="buttons"&gt;
          &lt;asp:Button runat="server" ID="btnProductDetails" OnCommand="btnProductDetails_Click"
              Text="Details" ValidationGroup="ProductDetails" CommandArgument='&lt;%# Eval("ProductID") %&gt;'
              SkinID="ProductGridProductDetailButton" /&gt;&lt;br /&gt;
          &lt;asp:Button runat="server" ID="btnAddToCart" OnCommand="btnAddToCart_Click" Text="Add to cart"
              ValidationGroup="ProductDetails" CommandArgument='&lt;%# Eval("ProductID") %&gt;' SkinID="ProductGridAddToCartButton" /&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>and the code behind is this:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
  
              // Some code here to generate a random number between 0 &amp; 3
              System.Random RandNum = new System.Random();
              int myInt = RandNum.Next(4);
  
              if (productPanel.BackImageUrl != null)
              {
                  switch (myInt)
                  {
                      case 0:
                          productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame1.gif";
                          break;
                      case 1:
                          productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame2.gif";
                          break;
                      case 2:
                          productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame3.gif";
                          break;
                      case 3:
                          productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame4.gif";
                          break;
                  }
  
              }
             // End of new code to switch background images
  
      }
  </code></pre>
  
  <p>T</p>