Search code examples
asp.netdata-bindingajaxcontroltoolkitrepeaterrating

Handling postbacks from an AJAX Rating Control inside a Repeater


I have a Repeater control with a Rating control (from the latest AJAX Control Toolkit) inside:

<asp:Repeater ID="repStudents" runat="server" onitemcommand="repStudents_ItemCommand">
  <ItemTemplate>
    <%# Eval("FirstName") %>
    <asp:Rating ID="warnings" runat="server" Direction="NotSet" MaxRating="3" StarCssClass="star" EmptyStarCssClass="em" FilledStarCssClass="gr" WaitingStarCssClass="gr" AutoPostBack="True" CommandArgument='<%# Eval("Id") %>' CommandName="warn"></asp:Rating>
    <br />
  </ItemTemplate>
</asp:Repeater>

In the code behind, I have:

protected void repStudents_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
  //Custom log function
  Log.Append(e.CommandName + " " + e.CommandArgument);
}

All renders fine. However, when I click on a rating the page posts back but repStudents_ItemCommand is not fired. How can I fix this?

Note that if I put a Button in the same Repeater, repStudents_ItemCommand fires correctly when I click the button.


Solution

  • The Rating control does not support CommandName/CommandArgument properties. But you can extend it as follows:

    public class ExRating : Rating {
    
        [Category("Behavior")]
        public string CommandName {
            get { return (string)ViewState["CommandName"] ?? string.Empty; }
            set { ViewState["CommandName"] = value; }
        }
    
        [Category("Behavior")]
        public string CommandArgument {
            get { return (string)ViewState["CommandArgument"] ?? string.Empty; }
            set { ViewState["CommandArgument"] = value; }
        }
    
        protected override void OnChanged(RatingEventArgs e) {
            base.OnChanged(e);
            RaiseBubbleEvent(this, new CommandEventArgs(CommandName, CommandArgument));
        }
    
    }
    

    Then replace the old control with new one:

    <pages>
      <tagMapping>
        <add tagType="AjaxControlToolkit.Rating, AjaxControlToolkit" mappedTagType="Sample.ExRating, Sample"/>
      </tagMapping>
    </pages>