I have a nested repeater that contains a CheckBox control.
<asp:Repeater runat="server" ID="optionalRepeater" OnItemDataBound="CheckCompCondiments">
<HeaderTemplate>
<div class="sectionHeader" style="font-weight: bold; font-size: 13pt; text-decoration: underline; margin: 20px 0px 15px 0px;">
Product Extras
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="col-md-4" style="border: 1px solid #ccc; margin: 5px;">
<asp:HiddenField runat="server" ID="salesMenuID" Value='<%#Eval("sales_menu_id") %>' />
<asp:HiddenField runat="server" ID="minVal" Value='<%#Eval("minVal") %>' />
<asp:HiddenField runat="server" ID="maxVal" Value='<%#Eval("maxVal") %>' />
<p>
<asp:Label runat="server" ID="condHdr" style="text-decoration: underline;" Text='<%#Eval("sales_menu_desc") %>'></asp:Label>
</p>
<asp:Repeater runat="server" ID="compMenuRadio">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdPriLvl"/>
<input type="radio" name="compSelect" runat="server"/>
</ItemTemplate>
<FooterTemplate>
<hr/>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater runat="server" ID="compMenuCheck">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdPriLvl" Value='<%#Eval("pPriceLvl") %>' />
<asp:CheckBox runat="server" ID='compSelected' CssClass='<%# Eval("pcode") %>' Text='<%# Eval("pdesc") %> '/>
<%--<input type="checkbox" id="compSelect" runat="server" value='<%#Eval("pCode") %>'/>--%>
<%-- <%#Eval("pDesc") %>--%>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
<asp:DropDownList runat="server" ID="compSelect" Visible="False" style="width: 98%; margin: 5px 0px 5px 0px;"/>
</div>
</ItemTemplate>
</asp:Repeater>
On my Server Side I have the following code:
if (optionalRepeater.Items.Count > 0)
{
foreach (RepeaterItem compItem in optionalRepeater.Items)
{
HtmlInputHidden priLvlVal = (HtmlInputHidden)compItem.FindControl("hdPriLvl");
foreach (Control compCtrl in compItem.Controls)
{
if (compCtrl.GetType() == typeof(CheckBox))
{
CheckBox checkBox = (CheckBox)compCtrl;
CheckBox chk = (CheckBox) checkBox;
if (checkBox.Checked)
{
string prod = checkBox.CssClass;
string childGuiud = Guid.NewGuid().ToString("N").ToUpper();
hasCondiment = true;
ShoppingCart.Instance.AddItem(prod, childGuiud, mainGuid, Session["transactionGuid"].ToString(), priLvlVal.Value, hasCondiment, 1, null);
checkBox.Checked = false;
}
}
}
}
}
My code get as far as this line
if (compCtrl.GetType() == typeof(CheckBox))
But never makes it inside the if statement. When I am expecting the type to be CheckBox it says literal Control.
Can anyone see anything that I could be doing wrong, I have been looking at this for a while now and I can't seem to get any further.
Thanks, Somango!
If you use an ItemTemplate
you have to use FindControl
to get the reference of the controls inside:
foreach (RepeaterItem compItem in optionalRepeater.Items)
{
HtmlInputHidden priLvlVal = (HtmlInputHidden)compItem.FindControl("hdPriLvl");
CheckBox compSelect = (CheckBox)compItem.FindControl("compSelect");
if (checkBox.Checked)
{
// ...
}
}
By looping the RepeaterItem.Controls
property you just find a single Literal
control which is the container for every control in the ItemTemplate
. So you could loop compItem.Controls[0].Controls
to find your CheckBox
. But in my opinion using compItem.FindControl
is much more readable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.