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.
The problem I have is that the code works 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.
The usercontrol is inside of a datalist.
The usercontrol is this:
<asp:Panel ID="productPanel" CssClass="ProductItem" runat="server">
<div class="title" visible="false">
<asp:HyperLink ID="hlProduct" runat="server" />
</div>
<div class="picture">
<asp:HyperLink ID="hlImageLink" runat="server" />
</div>
<div class="description" visible="false">
<asp:Literal runat="server" ID="lShortDescription"></asp:Literal>
</div>
<div class="addInfo" visible="false">
<div class="prices">
<asp:Label ID="lblOldPrice" runat="server" CssClass="oldproductPrice" />
<br />
<asp:Label ID="lblPrice" runat="server" CssClass="productPrice" /></div>
<div class="buttons">
<asp:Button runat="server" ID="btnProductDetails" OnCommand="btnProductDetails_Click"
Text="Details" ValidationGroup="ProductDetails" CommandArgument='<%# Eval("ProductID") %>'
SkinID="ProductGridProductDetailButton" /><br />
<asp:Button runat="server" ID="btnAddToCart" OnCommand="btnAddToCart_Click" Text="Add to cart"
ValidationGroup="ProductDetails" CommandArgument='<%# Eval("ProductID") %>' SkinID="ProductGridAddToCartButton" />
</div>
</div>
and the code behind is this:
protected void Page_Load(object sender, EventArgs e)
{
// Some code here to generate a random number between 0 & 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
}
T