我有一个函数,用于查找与转发器中每个产品项关联的复选框。如果选中该复选框,则int变量qty设置为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 use"><p class="hField">Use With</p></div>
<div id="shoppingHeader" class="headerField qty" runat="server"><p class="headerfield qty hField"><asp:Label id="list" runat="server" visible='<%# (showField()) %>' Text="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>
<div class="field use"><p class="pField"> <%# Eval("UseWith") %> </p></div>
<div id="shopping" class="field qty" runat="server"><p class="pField">
<asp:CheckBox Visible='<%# (showField()) %>' ID="LineQuantity" runat="server" /></p></div>
</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>
这是功能:
protected bool checkQtys(ref int ItemCnt)
{
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)
{
Response.Write("ct: " + ct.ToString() + "<br />");//This finds all controls except the checkboxes
if (ct is CheckBox)
{
CheckBox lineQty = (CheckBox)ct;
Label prodID = (Label)inner.FindControl("productID");
if (lineQty.Checked)
{
productID = prodID.Text;
qty = 1;
ItemCnt++;
}
}
}
}
}
}
}
}
}
return validQtys;
}
CheckBox不是一个控件,或者我是以错误的方式搜索它?
答案 0 :(得分:1)
通过查看代码,我不确定为什么要重复占位符的控件集合。您可以使用以下代码直接找到控件
CheckBox checkBox = skuItm.FindControl("LineQuantity") as CheckBox;
由于转发器项目&#34; skuItem&#34;是CheckBox的直接父命名容器,通过这种方式,您应该能够找到复选框