[英]ASP.NET CheckBox Control showing as a Literial
I have a nested repeater that contains a CheckBox control. 我有一个包含CheckBox控件的嵌套转发器。
<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. 但是永远不要在if语句中使用它。 When I am expecting the type to be CheckBox it says literal Control.
当我期望类型为CheckBox时,它会显示文字控件。
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! 谢谢,Somango!
If you use an ItemTemplate
you have to use FindControl
to get the reference of the controls inside: 如果使用
ItemTemplate
,则必须使用FindControl
来获取内部控件的引用:
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
. 通过循环
RepeaterItem.Controls
属性,您仅找到一个Literal
控件,它是ItemTemplate
每个控件的容器。 So you could loop compItem.Controls[0].Controls
to find your CheckBox
. 因此,您可以循环
compItem.Controls[0].Controls
来查找CheckBox
。 But in my opinion using compItem.FindControl
is much more readable. 但是我认为使用
compItem.FindControl
更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.