简体   繁体   English

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

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

I am trying to populate a repeater containing a label and a RadioButtonList in an ASP.NET webform to make a small quiz. 我正在尝试在ASP.NET网络表单中填充一个包含标签和RadioButtonList的中继器,以进行小测验。

These are the classes I am using: 这些是我正在使用的类:

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

}

and: 和:

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

}

My webform looks like this: 我的网络表单如下所示:

<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>

and the code-behind like so: 以及类似的代码如下:

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();

    }

For the time being I am just using one question. 目前,我只使用一个问题。 The label displaying my question shows up fine, but no radio buttons show up for displaying the answer options. 显示我的问题的标签显示正常,但是没有显示用于显示答案选项的单选按钮。 I have gone through many samples, and this seems to work for people when they use data from a DB via a DataTable or DataSet. 我经历了许多示例,当人们通过DataTable或DataSet使用来自DB的数据时,这似乎对人们有用。 However no matter how much I to play around with the Datasource, DataValueField and DataTextField parameters the RadioButtonList remains utterly barren. 但是,无论我要使用多少数据源,DataValueField和DataTextField参数,RadioButtonList仍然完全贫瘠。 As you can see, I initially used an array of Option and also tried a List but to no avail. 如您所见,我最初使用Option数组,还尝试了List,但无济于事。

What am I missing here? 我在这里想念什么?


EDIT - Found my mistake I had the <asp:label> and <asp:radiobuttonlist> tags nested wrong! 编辑-发现我的错误,我将<asp:label><asp:radiobuttonlist>标记嵌套错误! The label was encapsulating the radiobuttonlist and causing the problem from what I can make out. 标签正在封装单选按钮列表,并据我所知引起了问题。


ALTERNATIVE - thanks to balexandre 替代-感谢balexandre

balexandre did provide a workable alternative by using only code-behing. balexandre确实通过仅使用代码行为提供了可行的替代方案。 Thank you! 谢谢!

I am including the code listing of this alternative for anyone who needs it and happens across this post. 我将这种替代方法的代码清单提供给需要它的人,并且发生在本文中。

Change the markup to look like this: 更改标记如下所示:

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());
            }
        }
    }  

and the code-behind must have the event handler: 并且后面的代码必须具有事件处理程序:

 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()); } } } 

you can only access the template using the Repeater method OnItemDataBound witch is invoked before it draws anything to the page. 您只能使用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

    }
}    

Or by looping through all the Controls in the Repeater Collection, witch for example you have submited the page. 或者通过遍历Repeater集合中的所有控件,例如,您已提交页面。


BTW, and just for your information 顺便说一句, 仅供参考

this code: 此代码:

private string _body;
private bool? _isCorrect;

public string Body
{
    get { return _body; }
}

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

is the same as 是相同的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM