+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Search - control how much text to display

Search - control how much text to display

6 posts, 0 answered
  1. Ancuta Me
    Ancuta Me avatar
    43 posts
    Registered:
    26 Apr 2010
    19 Oct 2010
    Link to this post
    Hi everyone,

    Could someone tell me if there is a way to control how much text to show in a result item?
    For some items I get a sentence, for others I gen 4 rows of text... It doesn't look pretty.

    Thank you.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    19 Oct 2010
    Link to this post
    Hi Ancuta,

    You can set a limit to the snippet only programmatically. You can extend the SearchResults template - Sitefinity\ControlTemplates\Search\SearchResult.ascx. Inside the template you can subscribe for ItemDataBound event of the repeater and limit the snippet


    <script type="text/C#" runat="server">
       
        protected void Page_Load(object sender, EventArgs e)
        {
            this.rptResults.ItemDataBound += new RepeaterItemEventHandler(rptResults_ItemDataBound);
               
        }
       
        void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
       
                Literal lit = (Literal)e.Item.FindControl("Literal1");
                Telerik.Search.Engine.ResultItem resultITem = e.Item.DataItem as Telerik.Search.Engine.ResultItem;
                string value = resultITem.Snippet;
                
                 
                   int length = value.Length - toRemove.Length;
                   string value1 = value.Substring(value.IndexOf(toRemove) + toRemove.Length, length);
                   resultITem.Snippet = value1;
                   lit.Text = value1;                
            }
        }
       
    </script>

    You can add a new Literal control which will show the snippet of the result item and remove the default binder

    <dd><%#DataBinder.Eval(Container.DataItem, "Snippet")%></dd>

    Greetings,
    Ivan Dimitrov
    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
  3. Ancuta Me
    Ancuta Me avatar
    43 posts
    Registered:
    26 Apr 2010
    20 Oct 2010
    Link to this post
    Hi Ivan,

    I tried to implement your solution. I tried adding the snippet you provided to the SearchResult .ascx and removing the default binder and it did not work.

    I tried to create a new usercontrol to extend SearchResult but I have no class in the SearchResult behind code to add as being inherited.

    Could you please tell me where am I doing wrong?

    Thank you,
    Ancuta
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Oct 2010
    Link to this post
    Hi Ancuta,

    The suggested solution should work. Get the snippet as a a string and then subscribing the value returns
    a substring from this instance.

    All the best,
    Ivan Dimitrov
    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
  5. Ancuta Me
    Ancuta Me avatar
    43 posts
    Registered:
    26 Apr 2010
    21 Oct 2010
    Link to this post
    Hi Ivan ,

    I get an error at "toRemove", "The name toRemove does not exist in the current context".
    Is there something I'm missing?
    I followed your steps and added your code in a user control without code behind.

    Also, the value to which my content will be limited will be entered in the "Literal1" control? I'm not sure I understood that part.

    Thank you.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    21 Oct 2010
    Link to this post
    Hello Ancuta Me,

    You need to add a variable which specify the string lenght you want to get. You can use Substring(int, int) of the snippet. Then you need to set the returned value as a Text property of a new label control declared inside the template.

    All the best,
    Ivan Dimitrov
    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
Register for webinar
6 posts, 0 answered