This is a migrated thread and some comments may be shown as answers.

When binding Grid the Rate-event is fired a second time

3 Answers 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Susanna
Top achievements
Rank 1
Susanna asked on 06 Jan 2011, 07:25 PM
Hello. I have broken down my problem into a very simple page and I'm sure the solution is simple as well. I do need to rebind the grid (not in this example but in my real scenario I do) and my question is why the Rate event is triggered a second time when rebinding the grid? I have already handled the Rate event, saved my rating to the database, and as a final step I want to rebind the grid so that some other data is updated, for example some average rating or something like that. (My grid template column is much more advanced in the real scenario with lots of controls that needs to be updated when a new rating has been saved).

I've played around alot with simple binding, advanced binding and all sorts of things but right now my example looks like below. Please tell me how to solve this.

Here is the Code Behind    
public void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        FillGrid();
    }
}
 
public void FillGrid()
{
    RadGridItemRateSimple.DataSource = new ItemRateDTOService().GetItemsBySearchCriterion(new ItemSearchCriterion());
}
 
protected void RadGridItemRate_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    FillGrid();
}
 
protected void RadGridItemRate_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
    {
        ItemRateDTO itemRateDTO = (ItemRateDTO)e.Item.DataItem;
        RadRating radRating = (RadRating)e.Item.FindControl("RadRatingItem");
        if (radRating != null)
        {
            radRating.DbValue = CommonFunctions.SetDecimal(itemRateDTO.GetAvgRating());
        }
    }
}
 
protected void RadRatingItem_Rate(object sender, EventArgs e)
{
    RadRating radRating = (RadRating)sender;
    GridDataItem dataItem = (GridDataItem)radRating.Parent.Parent;
 
    long itemID = CommonFunctions.SetLong(RadGridItemRateSimple.MasterTableView.DataKeyValues[dataItem.ItemIndex]["Item.ID"]);
    Rating rating = new RatingService().GetSpecificRatingForUser(itemID, ActiveUserID);
 
    if (rating == null)
        rating = new Rating();
 
    rating.ItemID = itemID;
    rating.Rate = Convert.ToInt32(radRating.Value * 10);
    rating.CreatedBy = ActiveUserID;
 
    new RatingService().Save(rating);
     
    RadGridItemRateSimple.Rebind();
}

and the aspx
<telerik:RadGrid ID="RadGridItemRateSimple" OnItemDataBound="RadGridItemRate_ItemDataBound"  OnNeedDataSource="RadGridItemRate_NeedDataSource"
    runat="server" Width="850px" AllowSorting="True" AllowPaging="true" PagerStyle-Position="TopAndBottom" PageSize="30"
    AutoGenerateColumns="False" BorderWidth="0">
    <MasterTableView DataKeyNames="Item.ID">
        <Columns>
            <telerik:GridTemplateColumn ItemStyle-VerticalAlign="Top" HeaderText="" SortExpression="ItemName">
                <ItemTemplate>
                    <telerik:RadRating ID="RadRatingItem" runat="server" ItemCount="10" SelectionMode="Continuous"
                                    Precision="Half" Orientation="Horizontal" AutoPostBack="true" OnRate="RadRatingItem_Rate" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Thanks!

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 10 Jan 2011, 04:05 PM
Hello Carl,

Please try removing the call to the FillGrid method on Page_Load and let me know whether it helps:
public void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
         
    }
}

You can read more about advanced data-binding (using NeedDataSource event) here.

Kind regards,
Mira
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Susanna
Top achievements
Rank 1
answered on 10 Jan 2011, 11:10 PM
Hello, thanks for the answer.

I did remove the initial FillGrid but it did not help. I further simplified the example into only two lines of code that still repeats the double rate event. This is the only code I have now:

public partial class test : System.Web.UI.Page
{
    protected void RadGridItemRate_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGridItemRateSimple.DataSource = new ItemRateDTOService().GetItemsBySearchCriterion(new ItemSearchCriterion());
    }
 
    protected void RadRatingItem_Rate(object sender, EventArgs e)
    {
        RadGridItemRateSimple.Rebind();
    }
}

Problem is still there, as soon as I call Rebind and the datasource is set, the rate event is directly fired a second time. Oviously the example doesn't do anything usefull now as I don't bind any values to the rate controls or save them but it still shows the double event problem. ASPX looks the same as before (except I removed the ItemDataBound-event).

Any help would be apprechiated!
0
Tsvetie
Telerik team
answered on 13 Jan 2011, 02:46 PM
Hello Carl,
Thank you for going a step further and simplifying your code to that extent.

Let me first explain the nature of the Rate server-side event. It is neither PostBack event, nor data-changed event - it is both. Consider the following scenarios:
  1. The rating control initiates a postback, because its Value properties has changed and the Rate event fires.
  2. The rating control initiates a postback on every click on the control, because the developer wants to count all clicks on the control - different users can rate with the same rating a concrete article.

The problem that you have run into is a result of this dual nature of the Rate event. We are aware of it, but, unfortunately, have not yet found a way to work around this limitation. That is why, for cases like yours, you should not call the rebind method of RadGrid in the handler for the Rate event, but rather in its prerender event, when necessary - the simplest way, is to use a flag, just as we have done in our RadRating in RadGrid online demo - http://demos.telerik.com/aspnet-ajax/rating/examples/gridrating/defaultcs.aspx.

Greetings,
Tsvetie
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Susanna
Top achievements
Rank 1
Answers by
Mira
Telerik team
Susanna
Top achievements
Rank 1
Tsvetie
Telerik team
Share this question
or