簡體   English   中英

無法在ASP.NET Repeater模板中填充RadioButtonList

[英]Unable to populate RadioButtonList inside ASP.NET Repeater Template

我正在嘗試在ASP.NET網絡表單中填充一個包含標簽和RadioButtonList的中繼器,以進行小測驗。

這些是我正在使用的類:

public class Option
{
    private string _body;
    private bool? _isCorrect;

    #region properties
    public string Body
    {
        get { return _body; }
    }

    public bool? IsCorrect
    {
        get { return _isCorrect; }
    }

    #endregion

    #region constructors

    public Option(string body)
    {
        _body = body;
        _isCorrect = null;
    }

    public Option(string body, bool isCorrect)
    {
        _body = body;
        _isCorrect = isCorrect;
    }
    #endregion

    #region methods

    public override string ToString()
    {
        return _body;
    }

    #endregion

}

和:

public class Question
{
    private string _body;
    private Option[] _optionsArray;
    private List<Option> _optionsList;

    #region properties

    public string Body
    {
        get { return _body; }
    }

    public Option[] Options
    {
        get { return _optionsArray; }
    }

    public List<Option> OptionsList
    {
        get { return _optionsList; }

    }
    #endregion


    #region constructors

    public Question(string body, Option[] options)
    {
        _body = body;

        _optionsArray = options;

        _optionsList = new List<Option>();
        foreach (Option opt in options)
        {
            _optionsList.Add(opt);
        }
    }

    #endregion

    #region methods

    public override string ToString()
    {
        return _body;
    }

    public List<Option> GetOptions()
    {
        return OptionsList;
    }

    #endregion

}

我的網絡表單如下所示:

<div runat="server" ID="quizDiv">



    <br />
    <br />

    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <%--<asp:Label ID="questionBody" runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Body") %>>--%>
            <asp:Label ID="questionBody" runat="server" Text=<%# ((Question)Container.DataItem).Body %>>
            <asp:radiobuttonlist ID="blah" runat="server" DataTextField="Body" DataValueField="Body" DataSource=<%# ((Question)Container.DataItem).OptionsList %> >

            </asp:radiobuttonlist>  
            </asp:Label>
        </ItemTemplate>
    </asp:Repeater>
    <br />


</div>

以及類似的代碼如下:

protected void btnStart_Click(object sender, EventArgs e)
    {
        Dataset1 ds = new Dataset1();
        question = ds.CreateQuestion();

        List<Question> qts = new List<Question>();
        qts.Add(question);

        Repeater1.DataSource = qts;
        Repeater1.DataBind();

    }

目前,我只使用一個問題。 顯示我的問題的標簽顯示正常,但是沒有顯示用於顯示答案選項的單選按鈕。 我經歷了許多示例,當人們通過DataTable或DataSet使用來自DB的數據時,這似乎對人們有用。 但是,無論我要使用多少數據源,DataValueField和DataTextField參數,RadioButtonList仍然完全貧瘠。 如您所見,我最初使用Option數組,還嘗試了List,但無濟於事。

我在這里想念什么?


編輯-發現我的錯誤,我將<asp:label><asp:radiobuttonlist>標記嵌套錯誤! 標簽正在封裝單選按鈕列表,並據我所知引起了問題。


替代-感謝balexandre

balexandre確實通過僅使用代碼行為提供了可行的替代方案。 謝謝!

我將這種替代方法的代碼清單提供給需要它的人,並且發生在本文中。

更改標記如下所示:

protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("blah");
            // do anything with your rbl
            foreach (Option opt in question.Options)
            {
                rbl.Items.Add(opt.ToString());
            }
        }
    }  

並且后面的代碼必須具有事件處理程序:

 protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("blah"); // do anything with your rbl foreach (Option opt in question.Options) { rbl.Items.Add(opt.ToString()); } } } 

您只能使用Repeater方法訪問模板,然后再在頁面上繪制任何內容之前調用OnItemDataBound女巫。

void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

        RadioButtonList rbl = (RadioButtonList)e.Item.FindControl["blah"];
        // do anything with your rbl

    }
}    

或者通過遍歷Repeater集合中的所有控件,例如,您已提交頁面。


順便說一句, 僅供參考

此代碼:

private string _body;
private bool? _isCorrect;

public string Body
{
    get { return _body; }
}

public bool? IsCorrect
{
    get { return _isCorrect; }
}

是相同的

public string Body { private get; }
public bool? IsCorrect { private get; }

暫無
暫無

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

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