0

中继器代码:

<asp:Repeater ID="Repeater_sorular" runat="server" OnItemCommand="Repeater_sorular_ItemCommand"
        OnItemDataBound="Repeater_sorular_ItemBound">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <rad:RadChart ID="RadChart1" runat="server" DefaultType="Pie" Width="700" >
                            <PlotArea Appearance-FillStyle-FillType="Gradient" Appearance-FillStyle-MainColor="#D90420"
                                Appearance-FillStyle-SecondColor="#FFAD4A" Appearance-Border-Visible="false">
                                <EmptySeriesMessage>
                                    <TextBlock Text="Seçilen anket henüz oylanmamıştır.">
                                        <Appearance TextProperties-Font="Tahoma, 10pt, style=Bold">
                                        </Appearance>
                                    </TextBlock>
                                </EmptySeriesMessage>
                            </PlotArea>
                            <ChartTitle>
                                <TextBlock Appearance-TextProperties-Font="Tahoma">
                                </TextBlock>
                            </ChartTitle>
                            <Appearance ImageQuality="HighQuality" Border-Color="#DFDDDD" TextQuality="ClearTypeGridFit">
                            </Appearance>
                            <Series>
                                <rad:ChartSeries Type="Pie" Appearance-TextAppearance-TextProperties-Color="#FFFFFF"
                                    Appearance-TextAppearance-TextProperties-Font="Tahoma">
                                </rad:ChartSeries>
                            </Series>
                            <Legend Visible="True">
                                <TextBlock Visible="True">
                                </TextBlock>
                            </Legend>
                        </rad:RadChart>
                    </td>
                    <td>
                        <div style="font-weight: bolder; padding: 5px;">
                            <%#(((RepeaterItem)Container).ItemIndex+1).ToString() %>.
                            <%#Eval("Subject")%>
                        </div>
                        <asp:BulletedList ID="BulletedList_secenekler" runat="server" DataSource='<%#Eval("Secenekler")%>'
                            DataTextField="OptionName" DataValueField="OptionId" CssClass="sira_numarali">
                        </asp:BulletedList>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:Repeater>

中继器ItemDataBound:

protected void Repeater_sorular_ItemBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        int SurveyId = Int32.Parse(Request.QueryString["anket_id"]);

        var sorular = from sr in db.TableSurveyQuestions
                      where sr.SurveyId == SurveyId
                      select sr;

        //int repeater_satir = 0;
        foreach (var soru in sorular)
        {
            RadChart RadChart1 = new RadChart();
            RadChart1 = (RadChart)e.Item.FindControl("RadChart1");

            ChartSeries s = RadChart1.Series.GetSeries(0);
            s.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels;
            s.Clear();
            s.Appearance.ShowLabels = true;
            s.Appearance.LabelAppearance.Dimensions.Margins.Bottom = 7;
            s.PlotArea.IntelligentLabelsEnabled = true;
            s.DataYColumn = "VoteCount";

            int oy_sayisi = 0;

            foreach (var secenek in soru.TableSurveyOptions)
            {
                int toplam_cevap_sayisi = secenek.TableSurveyVotes.Count;
                int dogru_cevap_sayisi = secenek.TableSurveyVotes.Where(a => a.VoteStatus == true).Count();
                double yuzde = ((double)dogru_cevap_sayisi / (double)toplam_cevap_sayisi) * 100;

                ChartSeriesItem seriesItem = new ChartSeriesItem();
                seriesItem.YValue = Math.Round(yuzde, 2);
                seriesItem.Name = secenek.OptionName;
                seriesItem.ActiveRegion.Tooltip = secenek.OptionName;
                seriesItem.Label.TextBlock.Text = secenek.OptionName + " %" + Math.Round(yuzde, 2).ToString();
                seriesItem.Appearance.Border.Color = Color.Silver;
                seriesItem.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
                s.Items.Add(seriesItem);
                RadChart1.Series.Add(s);

                oy_sayisi = secenek.TableSurveyVotes.Count;
            }
            RadChart1.ChartTitle.TextBlock.Text = db.TableSurveyQuestions.Where(a => a.SurveyId == SurveyId).FirstOrDefault().TableSurvey.Title;
            RadChart1.Legend.TextBlock.Text = "Toplam : " + oy_sayisi + " Oy ";

            //repeater_satir++;
        }
    }
}

当我编写这段代码时,所有图表都显示了最后一个对象值。我想为每个图表设置不同的数据。

我怎样才能做到这一点。

谢谢。

4

1 回答 1

3

您可以使用该ItemIndex物业

<asp:Repeater runat="server" ID="myRepeater" OnItemDataBound="myRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:Label Text='<%# Eval("Id") %>' runat="server" ID="myRepeaterLabel" />
    </ItemTemplate>
</asp:Repeater>

后面的代码:

    protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            switch (e.Item.ItemIndex)
            {
                case 3:
                    Label l = (Label)e.Item.FindControl("myRepeaterLabel");
                    l.Text += "whatever";
                    break;
                default:
                    break;
            }

        }
    }

输出:

在此处输入图像描述

已编辑

我不确定这是否有帮助,但您应该像这样绑定中继器:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindRepeater();
        }
    }

    private void BindRepeater()
    {
        var r = Builder<Product>.CreateListOfSize(20).Build();
        this.myRepeater.DataSource = r;
        this.myRepeater.DataBind();
    }

并且只需修改事件中已经绑定的特定值ItemDataBound

于 2012-06-20T07:38:01.653 回答