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

Cannot get Rating events in my C# SharePoint webpart.

3 Answers 131 Views
Rating
This is a migrated thread and some comments may be shown as answers.
Mikael
Top achievements
Rank 1
Mikael asked on 21 Mar 2011, 02:39 PM
I've got a webpart which fetches items and their ratings from an SharePoint list. 

I've managed to show the rating for each item using the RadRating control. I've looked into the assembly using the Object Explorer and found only one System.EventHandler, named Rate. I've tried attaching my own event handler to handle the logic of getting the new value and passing that over to SharePoint, but for some reason I can't seem to figure out how to get the event from rating an item.

Here's a code snippet including how I display the RadRating and try to catch the events when the User rates by using the RadRating:
foreach (SPListItem answerItem in answerListItems)                  
                    {                                            
                        Literal QAContent = new Literal();  
                        RadRating QARating = new RadRating();
                        QAContent.Text  = "<div class='qaAnswer'>";
 
                        //<Snip>
 
                        if (answerItem["Luokitus_x0020__x0028_0_x0020__x"] != null)
                        {
                            try {
                                string ratingValue = answerItem["Luokitus_x0020__x0028_0_x0020__x"].ToString();
                                QARating.Value = Int32.Parse(ratingValue);
                                QARating.AutoPostBack = true;
                                QARating.Rate += new EventHandler(QARating_Rate);                               
 
                                pnlQuestios.Controls.Add(QARating);
                                 
                            }
                            catch {}                           
                        }

                        QAContentEnd.Text += "</div>";
                        pnlQuestios.Controls.Add(QAContentEnd);
                    }

And here's my QARating_Rate where I've put a breakpoint into my foo string for testing...
protected void QARating_Rate(object sender, EventArgs e)
{
    string foo = "foo";
}

The thing is, while debugging and clicking the stars in the browser, it won't hit the QARating_Rate method ever. What am I doing wrong?

3 Answers, 1 is accepted

Sort by
0
Niko
Telerik team
answered on 21 Mar 2011, 05:29 PM
Hi Mikael,

Please, find attached a slightly modified version of the code you provided. When the event as been called the string Rated should appear above the RadRating. All I added/changed were the loading of data, initializing a second Label instance and adding both of the Labels in the same panel above and below the rating:
var answerListItems = GetData();
 
foreach (var answerItem in answerListItems)
{
    Literal QAContent = new Literal();
    QAContent.Text = "<div class='qaAnswer'>";
    pnlQuestios.Controls.Add(QAContent);
             
    RadRating QARating = new RadRating();
 
    if (answerItem["Luokitus_x0020__x0028_0_x0020__x"] != null)
    {
        try
        {
            string ratingValue = answerItem["Luokitus_x0020__x0028_0_x0020__x"].ToString();
            QARating.Value = Int32.Parse(ratingValue);
            QARating.AutoPostBack = true;
            QARating.Rate += new EventHandler(QARating_Rate);
 
            pnlQuestios.Controls.Add(QARating);
        }
        catch
        {
        }
    }
    Literal QAContentEnd = new Literal();
    QAContentEnd.Text = "</div>";
    pnlQuestios.Controls.Add(QAContentEnd);
}
Modifications are highlighted.

Hope this helps.

Best wishes,
Niko
the Telerik team
0
Mikael
Top achievements
Rank 1
answered on 05 Apr 2011, 02:59 PM
Hi,

Sorry for my original code bit. It lacked those things that you added. I looked through your answer and the solution your provided. It was not complete as it lacked some things that weren't declared, so I couldn't run it. 

The biggest difference that I could see was the RadScriptManager that you added on the .aspx page. I tried to put it in my .ascx page, but loading the page results in an error stating "Only one instance of a ScriptManager can be added to the page". Apparenlty SharePoint 2010 already has a scriptmanager running. Do I need the RadScriptManager specificly to get the RadRating rate eventhandler to work?

I'm still pretty stuck, and hoping that you can provide some assistance. Here's a more full example of my code:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ShowQuestionsUserControl.ascx.cs" Inherits="Digia.MTVMedia.Intranet.UI.WebParts.MTVMediaShowQuestions.MTVMediaShowQuestionsUserControl" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI, Version=2010.3.1215.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %> 
<%@ Register Tagprefix="SharePointPortalControls" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  
  <SharePoint:CssRegistration ID="QAStyleCss" runat="server" Name="<% $SPUrl: ~sitecollection/Style Library/Styles/QAStyles.css %>" />
   <table>
   <tr>
    <td valign="top" height="40">
        <asp:TextBox ID="SearchBox" runat="server" CssClass="SearchBox" Width="200px"/>
        <asp:ImageButton ID="SearchButton"
            runat="server"
            CssClass="SearchButton"
            OnClick="SearchButton_Click"
            ImageUrl="<% $SPUrl:~sitecollection/Style Library/Images/search_qa.png %>" />
    </td>
    <td valign ="top">
        <a href ="#AddQ"><asp:Image ID="AddNewQBtn"
            runat="server"
            CssClass="SearchButton"          
            ImageUrl="<% $SPUrl:~sitecollection/Style Library/Images/lisaa_uusi_kysymys.png %>" /></a>
    </td>
        
   </tr>
   <tr>
    <td valign="top"><telerik:RadPanelBar ID="navigationByTopic" runat="server"></telerik:RadPanelBar></td>
    <td valign="top">
      <telerik:RadScriptManager ID="theScriptManager" runat="server" /> <!-- This causes a crash! -->
      <asp:Panel ID="pnlQuestios" runat="server">
      </asp:Panel>
      <asp:Panel ID="pnlAnswer" runat="server" Visible="false">
      </asp:Panel>
    </td>
   </tr>
  </table>          

Here's the codebehind. Basically it loads questions from a list based on the "meta" string and creates in code HTML elements to display the questions and answers.
private void loadQuestions(string meta)
{
    try
    {
        if (meta.Length == 0) return;
 
        //try to get list from Instructions/QA
        SPList myList = SPContext.Current.Web.Lists.TryGetList("QA");
        // list does not exist, then try in the root
        //development enviroment QA-list is in the root, but in test enviroment QA path is different
        if (myList == null)
        {
            SPWeb rootWeb = SPContext.Current.Site.RootWeb;
            myList = rootWeb.Lists.TryGetList("QA");
        }
 
        if (myList != null)
        {
 
            var answerListItems = from SPListItem tItem in myList.Items
                                  where tItem["Vastaus"] != null && tItem["Metatiedot"].ToString().Contains(meta) && tItem["Julkaisu"].ToString().Contains("Kyllä")
                                  select tItem;
 
        
 
             
            foreach (SPListItem answerItem in answerListItems)                  
            {                                            
                
                Literal QAContent = new Literal();  
                RadRating QARating = new RadRating();
                QAContent.Text  = "<div class='qaAnswer'>";
 
                Literal QAContentEnd = new Literal(); 
                 
                int intIndexQ = answerItem["Kysymys"].ToString().IndexOf('#');
                if (intIndexQ != 0)
                {
                    
                    string question = answerItem["Kysymys"].ToString().Substring(intIndexQ + 1);
                    QAContent.Text += "<span class='qaStrong'>Kysymys:</span><br/>" + SPEncode.HtmlEncodePreserveSpaces(question) + "<br/><br />";
                }
                else
                {
 
                    QAContent.Text += "<span class='qaStrong'>Kysymys:</span><br/>" + SPEncode.HtmlEncodePreserveSpaces(answerItem["Kysymys"].ToString()) + "<br/><br />";
                }
 
              
                if (answerItem["Vastaus"].ToString().Length >= 120)
                {
 
                    QAContent.Text += "<span class='qaStrong'>Vastaus:</span><br/>" + SPEncode.HtmlEncodePreserveSpaces(answerItem["Vastaus"].ToString().Substring(0, 120)) + "... ";
                    LinkButton lbReadMore = new LinkButton
                    {
                        Text = " Lue lisää",
                        ID = answerItem.UniqueId.ToString(),
                        CommandName = answerItem.ID.ToString()
                    };
                     
                    lbReadMore.Command += new CommandEventHandler(lb_Command);
                    lbReadMore.Click += new EventHandler(lbReadmore_Click);
 
                    pnlQuestios.Controls.Add(QAContent);                        
                    pnlQuestios.Controls.Add(lbReadMore);
                }
                else
                {
                    QAContent.Text += "<span class='qaStrong'>Vastaus:</span><br/>" + SPEncode.HtmlEncodePreserveSpaces(answerItem["Vastaus"].ToString());
                    pnlQuestios.Controls.Add(QAContent);
                    
                }
 
                if (answerItem["Luokitus_x0020__x0028_0_x0020__x"] != null)
                {
                    try {
                        string ratingValue = answerItem["Luokitus_x0020__x0028_0_x0020__x"].ToString();
                        QARating.Value = Int32.Parse(ratingValue);
                        QARating.AutoPostBack = true;
                        QARating.Rate += new EventHandler(QARating_Rate);                   

                        
pnlQuestios.Controls.Add(QARating);
                         
                    }
                    catch {}                           
                }
 
                QAContentEnd.Text += "<br><u>" + answerItem["Editor"].ToString().Split('#')[1] + "</u>";
                QAContentEnd.Text += "</div>";
                pnlQuestios.Controls.Add(QAContentEnd);
            }
        }
     
         
 
        pnlAnswer.Visible = false;
        pnlQuestios.Visible = true;
        SearchBox.Text = String.Empty;
    }
    catch (Exception)
    {
    }
 
}

        protected void QARating_Rate(object sender, EventArgs e)
        {
            string foo = "foo"; // don't ever hit this while debugging...
        }

0
Niko
Telerik team
answered on 07 Apr 2011, 02:18 PM
Hello Mikael,

You don't need the ScriptManager in the web part as it is already included in the SharePoint page. As far as the Rate event is concerned, please, find attached a sample web part that has implemented the programmatic creation of the RadRating control and attaching the Rate event handler.
Still the code that you provided, concerning the creating of the rating control and attaching it to the Rated event appears to be correct.

If you are unable to drill down the problem, I advice you to open a support ticket and attach a sample project with the necessary dependencies for us to test it and find out what may be causing the incorrect behavior you are reporting.

Kind regards,
Niko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Rating
Asked by
Mikael
Top achievements
Rank 1
Answers by
Niko
Telerik team
Mikael
Top achievements
Rank 1
Share this question
or