我有一个产品详情页面,其中有几个div设置为看起来像一张桌子。产品列表位于两个中继器之一。最外层的转发器(称为locationRepeater)为每批产品(公共区域,淋浴器和接收器)设置位置区域名称,第二个转发器(areaRepeater)生成产品列表(我无法返回并重做代码,所以我只使用1个转发器 - 没有时间):
<asp:Repeater ID="locationRepeater" runat="server" OnItemDataBound="SetInner">
<ItemTemplate>
<div class="LocationName">
<%# Eval("SecOpen") %><%# Eval("LocationName")%> <%# Eval("SecClose") %>
</div>
<asp:Repeater ID="areaRepeater" runat="server">
<HeaderTemplate>
<div class="headerRow">
<div class="header">
<div class="thumb"><p></p></div>
<div class="headerField name"><p class="hField">Product</p></div>
<div class="headerField sku"><p class="hField">GOJO SKU</p></div>
<div class="headerField size"><p class="hField">Size</p></div>
<div class="headerField case"><p class="hField">Case Pack</p></div>
<div class="headerField qty"><p class="hField">Add to Shopping List</p></div>
</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<asp:placeholder id="LocationAreaHeader" runat="server" visible='<%# (Eval("AreaName").ToString().Length == 0 ? false : true) %>' ><h3> <%# Eval("AreaName") %></h3></asp:placeholder>
<asp:placeholder id="ProductTable" runat="server" visible='<%# (Eval("ProductName").ToString().Length == 0 ? false : true) %>' >
<div class="table">
<div class="row">
<div class="thumb"><%# Eval("Charm") %></div>
<div class="field name"><p class="pField"> <%# Eval("ThumbOpen") %><%# Eval("ProductName") %><%# Eval("ThumbClose") %></p> </div>
<div class="field sku"><p class="pField"> <%# Eval("Sku") %> </p></div>
<div class="field size"><p class="pField"> <%# Eval("Size") %></p></div>
<div class="field case"><p class="pField"> <%# Eval("CasePack") %> </p></div>
<asp:PlaceHolder runat="server" ID="ShoppingField">
<div class="field qty"><p class="pField"> <asp:checkbox id="LineQuantity" runat="server" /></p></div>
</asp:PlaceHolder>
</div>
</div>
<asp:Label id="productID" text='<%# Eval("productID") %>' visible="false" runat="server" />
</asp:placeholder>
<!-- Stored values -->
<asp:Label id="SkuID" runat="server" text='<%# Eval("SkuID") %>' visible="true" />
<asp:Label id="masterSku" runat="server" text='<%# Eval("masterSku") %>' visible="false" />
<asp:Label id="masterName" runat="server" text='<%# Eval("masterName" ) %>' visible="false" />
<asp:Label ID="test" visible="false" runat="server" text='<%# Eval("AreaID") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:Label ID="refID" visible="false" runat="server" text='<%# Eval("LocationID") %>' />
</ItemTemplate>
</asp:Repeater>
在产品列表页面的模板中有一个名为&#34; Shopping Disabled&#34;的复选框。这是为了隐藏areaRepeater产品列表中的列(如果已选中)。然而,&#34;购物残疾人&#34;复选框存在于第一个转发器(locationRepeater)中。如何隐藏&#34;添加购物&#34;列基于在创建locationRepeater内找到的值?
private void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Item CurrentItem = Sitecore.Context.Item;
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"));
locations.Columns.Add("SecOpen", Type.GetType("System.String"));
locations.Columns.Add("SecClose", Type.GetType("System.String"));
string secColor = "";
if (CurrentItem.TemplateName == "gojoProductLocation")
{
Sitecore.Data.Fields.CheckboxField checkbox = CurrentItem.Fields["Active"];
if (checkbox.Checked)
{
DataRow dr = locations.NewRow();
dr["LocationName"] = CurrentItem.Fields["Header"].Value;
dr["LocationID"] = CurrentItem.ID.ToString();
locations.Rows.Add(dr);
}
}
else
{
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(@"child::*[@@templatename='gojoMarketOfficeBuildigProductMap']/*[@@templatename='gojoProductList']");
if (ProductGroup != null)
{
//this is where I can get the value of the "Shopping Disabled" checkbox
Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];
if (checkBox.Checked)
{
submitBtn.Visible = false;
}
Item[] LocationList = ProductGroup.Axes.SelectItems(@"child::*[@@templatename='gojoProductLocation' and @Active = '1']");
if (LocationList != null)
{
foreach (Item LocationItem in LocationList)
{
DataRow dr = locations.NewRow();
secColor = LocationItem.Fields["Section Color"].Value;
dr["SecOpen"] = "<h1 style='padding-top: 10px; background-color:#" + secColor + "'>";
dr["LocationName"] = LocationItem.Fields["Header"].Value;
dr["LocationID"] = LocationItem.ID.ToString();
dr["SecClose"] = "</h1>";
locations.Rows.Add(dr);
}
}
}
}
}
locationRepeater.DataSource = ds;
locationRepeater.DataMember = "locations";
locationRepeater.DataBind();
if (locationRepeater.Items.Count == 0)
{
//show message -RTE field on product map page
Literal lit = (Literal)FindControl("Return");
}
}
}
//This function populates the second repeater, which has the "Add to Shopping" column I want to hide
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");
string secColor = "";
areas.Columns.Add("AreaName", Type.GetType("System.String"));
areas.Columns.Add("Sku", Type.GetType("System.String"));
areas.Columns.Add("Large Image", Type.GetType("System.String"));
areas.Columns.Add("Charm", 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("CasePack", 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"));
areas.Columns.Add("ThumbOpen", Type.GetType("System.String"));
areas.Columns.Add("ThumbClose", 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"] = "N/A";
}
Sitecore.Data.Fields.XmlField fileField = ChildItem.Fields["Charm"];
newRow["Charm"] = "<image src=\"" + ScHelper.GetCorrectFilePath(fileField) + "\" border=\"0\" alt=\"\">";
newRow["Sku"] = MasterProduct.Fields["SKU"].Value;
newRow["productID"] = ChildItem.ID.ToString();
newRow["CasePack"] = MasterProduct.Fields["Case Pack"].Value;
newRow["Large Image"] = "";
try
{
string prodNameLink = "";
Item MasterProductName = db.Items[MasterProduct.Fields["Complete Product Name"].Value];
string prodImg = MasterProduct.Fields["Large Image"].Value;
if (prodImg != "")
{
Sitecore.Data.Fields.XmlField productImg = MasterProduct.Fields["Large Image"];
prodNameLink = MasterProduct.Fields["Large Image"].Value;
newRow["ThumbOpen"] = string.Format("<a href=\"{0}\" class=\"fancybox\" title=\"{1}\" rel=\"image\">", ScHelper.GetCorrectFilePath(productImg), MasterProduct.Fields["SKU"].Value);
newRow["ThumbClose"] = "</a>";
}
if (MasterProductName != null)
{
newRow["ProductName"] = MasterProductName.Fields["Complete Name"].Value;
}
areas.Rows.Add(newRow);
}
catch (Exception x)
{
Response.Write(x.Message.ToString() + "<br />");
}
}
}
}
}
areaRepeater.DataSource = dset;
areaRepeater.DataMember = "areas";
areaRepeater.DataBind();
}
}
}
}
答案 0 :(得分:0)
如果我理解正确,您希望隐藏一个表示为div的列?
如果是这种情况,请将属性runat="server"
添加到您要隐藏的div中。
在后面的代码中,您可以使用findcontrol查找复选框, areaRepeater 和div,然后您就可以根据复选框设置div的可见性。
这将是:
foreach(Repeater rep in locationRepeater.Items)
{
Repeater areaRepeater = (Repeater)rep.FindControl("areaRepeater");
CheckBox disable = (CheckBox)rep.FindControl("checkboxid");
foreach(Repeater areaRep in areaRepeater.Items)
{
HtmlGenericControl div = (HtmlGenericControl)areaRep.FindControl("div");
div.Visible = disable.Checked;
}
}
答案 1 :(得分:0)
可能有更好的方法来解决这个问题,但由于您已经在隐藏字段中使用存储数据的方法(例如:&#34; refID&#34;),您可以采用相同的方法进行购物禁用&#34;字段。
在Page_Load中添加一个新列到DataTable:
locations.Columns.Add("ShoppingDisabled", Type.GetType("System.String"));
将购物禁用数据添加到Page_Load中的此列:
dr["ShoppingDisabled"] = checkBox.Checked;
locations.Rows.Add(dr);
将隐藏标签添加到用户控件:
<asp:Label ID="ShoppingDisabled" visible="false" runat="server" text='<%# Eval("ShoppingDisabled") %>' />
在SetInner()方法中抓取标签:
Label lblShoppingDisabled = (Label)e.Item.FindControl("ShoppingDisabled");
为您的SetInner()方法或用户控件添加逻辑,以根据lblShoppingDisalbed.Text隐藏您需要隐藏的内容。
答案 2 :(得分:0)
好吧,那么,我最终做的是创建一个获得“Shopping Disabled”复选框值的函数,并根据它设置一个布尔变量为true或false。然后,我在“添加到购物清单”标题和产品清单表中的复选框字段的“可见”属性中调用此函数。这允许我根据需要显示/隐藏该列。谢谢你的建议!