简体   繁体   English

在转发器中的会话变量中显示项目?

[英]display items inside a session variable inside a repeater?

I am trying to get the items stored in a sessions variable into a repeater for users to see. 我正在尝试将存储在会话变量中的项目放入转发器中,以供用户查看。 However, I am not entirely sure how to do it (I'm new to session variables). 但是,我不完全确定该怎么做(我是会话变量的新手)。 Basically, when users enter in quantities for items on once page the hit submit, they are taken to an "order summary" page, which will display what they plan to purchase. 基本上,当用户在点击提交的第一个页面上输入商品数量时,他们将被带到“订单摘要”页面,该页面将显示他们打算购买的商品。 I have successfully set up a session variable to contain the sku and quantity of each product the user selects, but I do not know how to get the information out. 我已经成功设置了一个会话变量,以包含用户选择的每种产品的sku和数量,但是我不知道如何获取信息。

I've stored the information in the session variable as [sku],[quantity];[sku],[quantity];[sku],[quantity] and so on. 我已将信息存储在会话变量中,分别为[sku],[quantity],[sku],[quantity],[sku],[quantity]等。 I figure I must do a split or something based on the commas and semicolons but I am not sure how to do so with a session variable. 我认为我必须根据逗号和分号进行拆分或其他操作,但是我不确定如何使用会话变量进行拆分。

The code for the product listing page that contains the information to be stored in the session variable: 产品清单页面的代码,其中包含要存储在会话变量中的信息:

public partial class GojoptproductlistSublayout : System.Web.UI.UserControl
{
    private void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            Item CurrentItem = Sitecore.Context.Item;

            Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");

            if (HomeItem != null)
            {
                Item ProductGroup = HomeItem.Axes.SelectSingleItem(@"child::*[@@templatename='gojoMarketOfficeBuildigProductMap']/*[@@templatename='gojoProductList']");

                if (ProductGroup != null)
                {
                    Item[] LocationList = ProductGroup.Axes.SelectItems(@"child::*[@@templatename='gojoProductLocation' and @Active = '1']");
                    if (LocationList != null)
                    {
                        DataSet ds = new DataSet();
                        DataTable locations = ds.Tables.Add("locations");

                        locations.Columns.Add("LocationName", Type.GetType("System.String"));
                        locations.Columns.Add("LocationID", Type.GetType("System.String"));

                        foreach (Item LocationItem in LocationList)
                        {
                            DataRow dr = locations.NewRow();
                            dr["LocationName"] = LocationItem.Fields["Header"].Value;
                            dr["LocationID"] = LocationItem.ID.ToString();
                            locations.Rows.Add(dr);
                        }

                        locationRepeater.DataSource = ds;
                        locationRepeater.DataMember = "locations";
                        locationRepeater.DataBind();
                    }
                }
            }
        }
    }

    protected void SetInner(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Footer) & (e.Item.ItemType != ListItemType.Header))
        {
            Label refID = (Label)e.Item.FindControl("refID");
            Label test = (Label)e.Item.FindControl("test");
            Repeater areaRepeater = (Repeater)e.Item.FindControl("areaRepeater");

            Database db = Sitecore.Context.Database;
            Item LocationAreaItem = db.Items[refID.Text];

            if (LocationAreaItem != null)
            {
                Item[] AreaList = LocationAreaItem.Axes.SelectItems(@"child::*[@@templatename='gojoProductLocationArea' and @Active = '1']");

                if (AreaList != null)
                {
                    DataSet dset = new DataSet();
                    DataTable areas = dset.Tables.Add("areas");

                    areas.Columns.Add("AreaName", Type.GetType("System.String"));
                    areas.Columns.Add("Sku", Type.GetType("System.String"));
                    areas.Columns.Add("ProductName", Type.GetType("System.String"));
                    areas.Columns.Add("masterSku", Type.GetType("System.String"));
                    areas.Columns.Add("masterName", Type.GetType("System.String"));
                    areas.Columns.Add("Size", Type.GetType("System.String"));
                    areas.Columns.Add("SkuID", Type.GetType("System.String"));
                    areas.Columns.Add("AreaID",Type.GetType("System.String"));
                    areas.Columns.Add("productID", Type.GetType("System.String"));

                    foreach (Item AreaItem in AreaList)
                    {
                        DataRow drow = areas.NewRow();

                        drow["AreaName"] = AreaItem.Fields["Header"].Value;
                        drow["AreaID"] = AreaItem.ID.ToString();

                        areas.Rows.Add(drow);

                        Item[] SkuList = AreaItem.Axes.SelectItems(@"child::*[(@@templatename='gojoPTRefill' or @@templatename = 'gojoPTAccessories' or  @@templatename = 'gojoPTDispenser' or @@templatename = 'gojoPTSelfDispensed') and @Active = '1']");

                        foreach (Item ChildItem in SkuList)
                        {
                            Item MarketProduct = db.Items[ChildItem.Fields["Reference SKU"].Value];
                            drow["productID"] = ChildItem.ID.ToString();

                            if (MarketProduct != null)
                            {
                                Item MasterProduct = db.Items[MarketProduct.Fields["Master Product"].Value];
                                if (MasterProduct != null)
                                {
                                    DataRow newRow = areas.NewRow();

                                    if(MasterProduct.TemplateName == "gojoSKUSelfDispensed" || MasterProduct.TemplateName == "gojoSKURefill")
                                    {
                                        newRow["Size"] = MasterProduct.Fields["Size"].Value;
                                    }
                                    else
                                    {
                                        newRow["Size"] = "-";
                                    }

                                    newRow["Sku"] = MasterProduct.Fields["SKU"].Value;
                                    newRow["productID"] = MasterProduct.ID.ToString();

                                    Item MasterProductName = db.Items[MasterProduct.Fields["Complete Product Name"].Value];

                                    if (MasterProductName != null)
                                    {
                                        newRow["ProductName"] = MasterProductName.Fields["Complete Name"].Value;

                                    }
                                    areas.Rows.Add(newRow);
                                }
                            }
                        }
                    }
                    areaRepeater.DataSource = dset;
                    areaRepeater.DataMember = "areas";
                    areaRepeater.DataBind();
                }

            }
        }
    }

    protected bool checkQtys(ref int ItemCnt, ref  ArrayList LinesToOrder)
    {
        Repeater locationRepeater = (Repeater)FindControl("locationRepeater");

        bool validQtys = true;
        string productID = "";
        int qty;
        qtyErrorMsg.Text = "";
        qtyErrorMsgTop.Text = "";
        foreach (RepeaterItem repItem in locationRepeater.Items)
        {
            if (repItem != null)
            {
                Repeater areaRepeater = (Repeater)repItem.FindControl("areaRepeater");
                if (areaRepeater != null)
                {
                    foreach (RepeaterItem skuItm in areaRepeater.Items)
                    {

                        if (skuItm != null)
                        {
                            Label SkuID = (Label)skuItm.FindControl("SkuID");
                            Label qtyID = (Label)skuItm.FindControl("qtyID");

                            PlaceHolder inner = (PlaceHolder)skuItm.FindControl("ProductTable");

                                if (inner != null)
                                {
                                    foreach (Control ct in inner.Controls)
                                    {
                                        if (ct is TextBox)
                                        {
                                            TextBox lineQty = (TextBox)ct;
                                            Label prodID = (Label)inner.FindControl("productID");

                                            if (lineQty.Text != "")
                                            {
                                                try
                                                {
                                                    int.Parse(lineQty.Text);
                                                    productID = prodID.Text;
                                                    qty = int.Parse(lineQty.Text);

                                                    if (qty > 0)
                                                    {
                                                        noItemMsg.Visible = false;
                                                        noItemMsgTop.Visible = false;
                                                        ItemCnt++; //only count items with valid qty values
                                                        LinesToOrder.Add(new LineItem(productID, qty));
                                                    }
                                                    else
                                                    {//Qty is 0 or less error
                                                        validQtys = false;
                                                        qtyErrorMsg.Text = "Quantity must be a number<br />";
                                                        qtyErrorMsgTop.Text = "Quantity must be a number<br />";
                                                    }

                                                }
                                                catch
                                                {//NaN - display error msg
                                                    validQtys = false;
                                                    qtyErrorMsg.Text = "Quantity must be a number<br />";
                                                    qtyErrorMsgTop.Text = "Quantity must be a number<br />";
                                                }
                                            }

                                        }

                                    }

                                }
                        }

                    }
                }

            }
        }
        return validQtys;
    }

    class LineItem
    {//This class will store the product information
        public string SKUID;
        public int Qty;

        public LineItem(string InSKUID, int InQty)
        {
            this.SKUID = InSKUID;
            this.Qty = InQty;
        }
    }

    protected void orderSubmit(object sender, EventArgs e)
    {
        int ItemCnt = 0;
        bool validQtys = true;
        ArrayList LinesToOrder = new ArrayList();
        Label lb = FindControl("order") as Label;

        if (checkQtys(ref ItemCnt,ref LinesToOrder))
        {
            if (ItemCnt == 0)
            {//make sure at least one item with proper qty amount is entered before submitting the order
                validQtys = false;
                noItemMsg.Visible = true;
                noItemMsg.Text = "You must order at least one item<br />";
                noItemMsgTop.Visible = true;
                noItemMsgTop.Text = "You must order at least one item<br />";
            }
            if (validQtys)
            {//save the information to a session variable and send users to order review page      
                try
                {
                    foreach (LineItem WorkLine in LinesToOrder)
                    {
                        lb.Text += WorkLine.SKUID + ", " + WorkLine.Qty + ";";
                    }

                    Session["orderComplete"] = lb.Text;
                }

                catch (Exception x)
                {
                    Response.Write(x.Message.ToString());
                }
                Response.Redirect("/united-states/market/office-buildings/obproductmap/OrderReview");

            }
        }
    }
}

Here is the designer code with the repeater that is meant to hold the order summary: 这是带有转发器的设计器代码,用于保存订单摘要:

<asp:Repeater ID="orderRepeater" runat="server" >
    <headertemplate>
        <tr>
            <th>SKU</th>
            <th>Quantity</th>
        </tr>
    </headertemplate>
    <itemtemplate>
        <tr>
            <td><%#Eval("sku") %></td>
            <td><%#Eval("qty") %></td>
        </tr>
    </itemtemplate>
</asp:Repeater>

And here is the code behind: 这是后面的代码:

private void Page_Load(object sender, EventArgs e) 
{
    Item CurrentItem = Sitecore.Context.Item;
    Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");

    if (Session["orderComplete"] != "")
    {
        if (HomeItem != null)
        {
            Item ProductGroup = HomeItem.Axes.SelectSingleItem(@"child::*[@@templatename='gojoMarketOfficeBuildigProductMap']/*[@@templatename='gojoOrderReview']");

            if (ProductGroup != null)
            {
               //this is where I am confused on how to proceed


            }

        }
    }
}

Everything is working and I did a Response.Write test on the session variable to make sure it had the correct information and it did. 一切正常,我对会话变量进行了Response.Write测试,以确保它具有正确的信息并且确实如此。

Thanks in advance! 提前致谢!

Before I try to address your question, lets take a look at some basics. 在尝试解决您的问题之前,让我们看一些基本知识。 A Session can hold an object, not just a string object. 会话可以保存一个对象,而不仅仅是一个字符串对象。 I would change: 我会改变:

 if (Session["orderComplete"] != "")

to

if (Session["orderComplete"] != null && Session["orderComplete"] != "")

if you don't, and Session["orderComplete"] is null, Session["orderComplete"] != "" will throw an error object not set to an instance of an object 如果您不这样做,并且Session["orderComplete"]为null,则Session["orderComplete"] != ""将抛出一个错误object not set to an instance of an object

And now to your question. 现在是您的问题。 Setting your session variable to [sku],[quantity];[sku],[quantity];[sku],[quantity] , is not a good idea. 将会话变量设置为[sku],[quantity];[sku],[quantity];[sku],[quantity]并不是一个好主意。 For one, its not object oriented and 2, its not going to bind to any repeater or data source. 对于它,它不是面向对象的,而对于2,它是不会绑定到任何转发器或数据源的。 You should create an object and bind a list of those objects to your control: 您应该创建一个对象并将这些对象的列表绑定到控件:

pseudo code: 伪代码:

class Program
{
 static void Main(string[] args)
 {
     List<Order> orders = new List<Order>();
     orders.Add(new Order { Sku = "ABC", Qty = 10});

 }
}

public class Order {

 public String Sku { get; set; }
 public int Qty { get; set; }

}

Then you can bind orders to your repeater. 然后,您可以将orders绑定到中继器。 For example: 例如:

 if (Session["orderComplete"] != null && Session["orderComplete"] != ""){
     List<Order> orders = Session["orderComplete"] as List<Order>;
     myRepeater.DataSource = orders;
     myRepeater.DataBind();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM