簡體   English   中英

如何在頁腳模板數據列表中獲取單選按鈕檢查值?

[英]How to get radio button checked value in Footer Template DataList?

我正在使用DataList。在我的FooterTemplate中,我添加了一個單選按鈕II,希望對其進行檢查。但是它始終返回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
{
}

如何解決?

嘗試這個

        foreach (DataListItem item in dlDelivery.Items)
        {
            if (item.ItemType == ListItemType.Footer)
            {
                RadioButton rdoOther = (RadioButton)item.FindControl("rdoOther");

            }
        }

只需在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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM