0

I've got my own user control inside repeater. It displays stars according to list element rating. On the first bind everything is ok, number of stars is ok for each item. Then I make a postback in which I filter this list. Then result data bind to the repeater. Lets say at first I had 5 elemetns after filtering only 3 but and only 3 are visible but the stars are in order as they were before binding.

What I would like to have is during postback each one control which is inside repeater should be recreated and appropriate number of stars whould be displayed (not the old one).

What am I doing wrong?

The repeater is inside an updatePanel.

here is my control "stars" code:

<span runat="server" id="starHolder" class="starsHolder" title="">
    <input runat="server"  id="star1" name="star3" type="radio" class="star1"  value="1"  />
    <input runat="server"  id="star2" name="star3" type="radio" class="star1"  value="2"  />
    <input runat="server"  id="star3" name="star3" type="radio" class="star1"  value="3" />
    <input runat="server"  id="star4" name="star3" type="radio" class="star1"  value="4"/>
    <input runat="server"  id="star5" name="star3" type="radio" class="star1"  value="5"/>
</span>

and here is my repeater code:

<asp:Repeater EnableViewState="false" OnItemDataBound="RptrTopRated_OnDataItemBound" ID="rptrTopRated" runat="server">
    <ItemTemplate>
    <li class="topRated" id='<%#DataBinder.Eval(Container.DataItem, "[id]")%>'>
        <p>
            <%# DataBinder.Eval(Container.DataItem, "[title]") %>
        </p>
        <span class="ratingStar">
            <cms:stars EnableViewState="false" runat="server" id="ctrlRating" McID='<%#System.Convert.ToInt32(DataBinder.Eval(Container.DataItem, "[id]"))%>' Mode="small"></cms:stars>   
        </span>    
        <a href="#" class="greenLink png" title="Leer más">Leer más</a>
    </li>
    </ItemTemplate>
</asp:Repeater>

It doesn't matter if enableViewsstate is set to true or false.

On ItemBound event of the repeater I even set the code:

protected void RptrTopRated_OnDataItemBound(object sender, RepeaterItemEventArgs e)
    {
        ((stars)e.Item.FindControl("ctrlRating")).SetStarsRating();
    }

which determines which input should have attrbute("checked") selected to checked:

public void SetStarsRating() {
    int rate = GetRate();
            this.starHolder.Attributes["title"] = McID.ToString();
            if(star1.Attributes["checked"] != null) star1.Attributes.Remove("checked");
            if(star2.Attributes["checked"] != null) star2.Attributes.Remove("checked");
            if(star3.Attributes["checked"] != null) star3.Attributes.Remove("checked");
            if(star4.Attributes["checked"] != null) star4.Attributes.Remove("checked");
            if(star5.Attributes["checked"] != null) star5.Attributes.Remove("checked");

            switch (rate)
            {
                case 1:
                    star1.Attributes["checked"] = "checked";
                    break;
                case 2:
                    star2.Attributes["checked"] = "checked";
                    break;
                case 3:
                    star3.Attributes["checked"] = "checked";
                    break;
                case 4:
                    star4.Attributes["checked"] = "checked";
                    break;
                case 5:
                    star5.Attributes["checked"] = "checked";
                    break;
            }
}

During debugging I can see that code behaves in right way and sets checked attribute on good one star but there is no result in rendered HTML.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
gruber
  • 28,739
  • 35
  • 124
  • 216
  • 1
    Try removing UpdatePanel – Brian Webster Jul 15 '11 at 22:35
  • I dont think that you are serious – gruber Jul 15 '11 at 22:38
  • Do you have the source code for this control? Can you change it if need be? – pbz Jul 15 '11 at 22:38
  • i just want to know why there are always so many prpblems with dynamic controls inside repeaters, update panels and so on ... – gruber Jul 15 '11 at 22:39
  • I do have a source code and I can change it – gruber Jul 15 '11 at 22:39
  • Depending on how it works you could try putting the SetStarsRating in OnPreRender or even Render. This way you wouldn't need to call SetRender "manually". Why it doesn't render has probably something to do with how that control maintains its internal organization. Does it use child controls? Are these children added to the collection? Where are they added? Etc... If possible post the code for that control... – pbz Jul 15 '11 at 22:44
  • "many prpblems with dynamic controls inside repeaters" -- most issues I've seen is with controls that have not been written properly -- it's fairly difficult to write good control code, so that's probably why – pbz Jul 15 '11 at 22:46
  • @gruber let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1526/discussion-between-pbz-and-gruber) – pbz Jul 15 '11 at 22:46
  • 2
    @gruber Many, Many things break inside of update panels. Have you tried removing it? If you have, and it works without the update panel, the we're one step closer to fixing it. – Brian Webster Jul 15 '11 at 22:50

2 Answers2

1

You might consider using ASP.Net Ajax Rating control, which will save you a lot of time.

http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Rating/Rating.aspx

Win
  • 61,100
  • 13
  • 102
  • 181
  • @gruber it is a legitimate answer. You failed to mention that restriction. Please check your attitude, you'll get a better response. – Kyle Trauberman Jul 15 '11 at 22:55
  • im writing about the problem and the one writes about something completely different, sorry not to mention its senseless – gruber Jul 15 '11 at 23:00
  • 1
    @gruber perhaps, but there has to be a better way to say it than "you have got to be kidding me" – Kyle Trauberman Jul 15 '11 at 23:15
0

I added method in Render in my star control and it started to work as it should:

 protected override void Render(HtmlTextWriter writer){
        SetStarsRating();
        base.Render(writer);
    }
gruber
  • 28,739
  • 35
  • 124
  • 216