我正在使用DataList。在我的FooterTemplate中我添加一个单选按钮我想检查它。但它总是返回false。
这是我的代码
<asp:DataList ID="dlDelivery" OnItemDataBound="dlDelivery_DataBound" RepeatColumns="1" RepeatDirection="Vertical" Width="300" runat="server">
<ItemTemplate>
<asp:RadioButton ID="rdoDel" GroupName="aaa" OnCheckedChanged="rdoOther_Changed" AutoPostBack="true" runat="server" />
<asp:Label ID="lblDel1" Text='<%# Eval("Street") %>' runat="server" /><br />
<asp:Label ID="lblDel2" Text='<%# Eval("Suburb") %>' runat="server" />
<span class="clear" />
</ItemTemplate>
<FooterTemplate>
<asp:RadioButton ID="rdoOther" Text="Other" OnCheckedChanged="rdoOther_Changed" AutoPostBack="true" GroupName="aaa" runat="server" />
a
<br class="clear" />
</FooterTemplate>
</asp:DataList>
我检查了这样的rdoOther检查
RadioButton rdoOther = (RadioButton)dlDelivery.Controls[dlDelivery.Controls.Count - 1].Controls[0].FindControl("rdoOther");
if (rdoOther.Checked = true ) // this always fales
{
}
如何解决?
答案 0 :(得分:1)
试试这个
foreach (DataListItem item in dlDelivery.Items)
{
if (item.ItemType == ListItemType.Footer)
{
RadioButton rdoOther = (RadioButton)item.FindControl("rdoOther");
}
}
答案 1 :(得分:0)
只需搜索e.Item中的单选按钮:
protected void dlDelivery_DataBound(object sender, DataListItemEventArgs e)
{
RadioButton rdoOther = e.Item.FindControl("rdoOther") as RadioButton;
//rdoOther will be null if ListItemType is not footer.
if (rdoOther !=null && rdoOther.Checked)
{
// Do your tasks
}
}