0

I want to build a poll system in asp.net with Repeater control, in DB has 2 table (Polls, PollItems). and use this code for select poll:

var lstPoll = (from P in Conn.Polls.ToList()
                            orderby P.PollOrder
                            select new { P.Question, P.PollID, P.PollItems }).ToList();

RepeaterPoll.DataSource = lstPoll.ToList();
RepeaterPoll.DataBind();

PollItems table (P.PollItems) has PollItemID, PollAnswer parametr.

<asp:Repeater ID="RepeaterPoll" runat="server">
    <ItemTemplate>
    <%# Eval("Question")%>
    <br />
    <asp:RadioButtonList ID="rblItemPoll" runat="server" DataValueField='<%# Eval("PollItemID")%>' DataTextField='<%# Eval("PollAnswer")%>'></asp:RadioButtonList>
    <br /><br />
    </ItemTemplate>
    </asp:Repeater>

but not Eval PollAnswer and PollItemID to rblItemPoll.

plase help me for build poll system.

4

1 回答 1

2

Try this:

1 - Define your Repeater with the RadioButtonList control and specifying an OnItemDataBound event handler:

<asp:Repeater ID="RepeaterPoll" runat="server" OnItemDataBound="RepeaterPoll_OnItemDataBound">
    <ItemTemplate>
        <%# Eval("Question")%>
        <br />
        <asp:RadioButtonList ID="rblItemPoll" runat="server" />
        <br /><br />
    </ItemTemplate>
</asp:Repeater>

2- Implement your logic of DataBinding the RadioButtonList on the OnItemDataBound event:

protected void RepeaterPoll_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        dynamic item = e.Item.DataItem;
        RadioButtonList list = (RadioButtonList)e.Item.FindControl("rblItemPoll");
        list.DataValueField = "PollItemId";
        list.DataTextField = "PollItemDescription";
        list.DataSource = item.PollItems;
        list.DataBind();
    }
}

You can define both DataValueField and DataTextField properties directly on the RadioButtonList tag tho.

Additionally, i retrieved the DataItem as dynamic because you are creating an anonymous type for your LINQ query result.

于 2013-03-05T21:11:29.010 回答