As it stands right now, I have a literal control on my page. In my code-behind, I'm using StringBuilder
to generate some JavaScript. Also on that page I have a item collection. What I want to do is for each item in my item collection, generate my literal which will in essence generate my JavaScript. Here is an example of my code-behind now. I'm ok with doing something different, but I just need to generate said JavaScript for every item in the collection and I'm not sure how to do it.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type='text/javascript'>");
sb.Append("mboxCreate(\"product_productpage_rec\",");
sb.Append("\"entity.id=" + _prodID + "\",");
sb.Append("\"entity.categoryId=" + _categoryID + "\",");
sb.Append("\"entity.name=" + _prod.Title + "\",");
sb.Append("\"entity.pageURL=" + Request.Url.ToString() + "\",");
//The following value has been taken from the productImageControl code behind.
//Might have to refactor in future as a property of the image control.
string filename = AppSettingsManager.Current.ProductImagePathLarge + _prod.ActiveProductItemCollection[0].Sku + AppSettingsManager.Current.ProductImageExtension;
sb.Append("\"entity.thumbnailURL=" + filename + "\",");
sb.Append("\"entity.inventory=" + _prod.ActiveProductItemCollection.Count + "\",");
sb.Append("\"entity.value=" + _prod.ActiveProductItemCollection[0].ActualPrice + "\",");
sb.Append("\"entity.ProductItemID=" + prodItem.Id + "\",");
sb.Append("\"entity.addToCartImg=~/Images/Buttons/btn_AddToCartFlat.gif\");<");
//The last line has to be /script. < inserted on prev line. do not change it or bad things will happen.
sb.Append("/script>");
//add script to page
this.LiteralMBoxScript.Text = sb.ToString();