mvc 5 c# html color individual characters of string

4 posts, 0 answers
  1. Arun
    Arun avatar
    15 posts
    Member since:
    Oct 2014

    Posted 30 Nov 2015 Link to this post

    In c# , I am trying to set font for individual characters of string.

    Below is my code that I use with Kendo Grid to display tree node text which displays some count in brackets. 

    Example Customer1(**20**). Here I want to bold and color count.

         Name = EmpDb.Description + ((EmpDb.Cust.Count() > 0) ? "(" : "") +
                                      ((EmpDb.Cust.Count() > 0) ? EmpDb.Cust.Count().ToString() : "") +
                                     ((EmpDb.Cust.Count() > 0) ? ")" : "")

    I tried below to bold, but it did not work and I got html tags in string. 

         Name = EmpDb.Description + ((EmpDb.Cust.Count() > 0) ? "(<b>" : "") +
                                      ((EmpDb.Cust.Count() > 0) ? EmpDb.Cust.Count().ToString() : "") +
                                     ((EmpDb.Cust.Count() > 0) ? "</b>)" : "")

    Thank you.
  2. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    356 posts

    Posted 02 Dec 2015 Link to this post

    Hi Arun,

    My understanding is that you want to display HTML markup inside the TreeView items, and the widget is server-bound. In this case, the easiest thing to do is disable TreeView item encoding.

    @(Html.Kendo().TreeView()
        .Name("treeview")
        .Items(treeview =>
        {
            treeview.Add().Text("<b>Text</b>").Encoded(false);
     
        })
    )


    In case your scenario is different, please provide more information and clarify what is the relationship between the Grid and the TreeView, and how is the TreeView related to the provided server code.

    Regards,
    Dimiter Topalov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Arun
    Arun avatar
    15 posts
    Member since:
    Oct 2014

    Posted 02 Dec 2015 Link to this post

    Thanks for reply.

     Actually I am doing similar to example at http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/treeview/ajax-binding

     So in the example exactly similar to above, for my Jason result, Name value is retrevied as

     Name = EmpDb.Description + ((EmpDb.Dept.Count() > 0) ? " (" : "") +

                                      ((EmpDb.Dept.Count() > 0) ? EmpDb.Dept.Count().ToString() : "") +
                                     ((EmpDb.Dept.Count() > 0) ? ")" : "")

     So my question is for above how can i set  font for value between bracket.

    for example If I have  treeview node text as

     Sam (5)

    then I want value 5 to be bold and in color.

     

    Thank You.

  5. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    356 posts

    Posted 07 Dec 2015 Link to this post

    Hello Arun,

    You can try to wrap the value in the brackets in a <strong> tag like in the following example:

    Name = EmpDb.Description + ((EmpDb.Dept.Count() > 0) ? "(<strong>" : "") +
            ((EmpDb.Dept.Count() > 0) ?
        EmpDb.Dept.Count().ToString() :
    "") +
             ((EmpDb.Dept.Count() > 0) ? "</strong>)" : "")

    And then disable the encoding for the corresponding TreeView item:

    @(Html.Kendo().TreeView()
        .Name("treeview")
        .Items(treeview =>
        {
            treeview.Add().Text("<b>Text</b>").Encoded(false);
     
        })
    )

    If this doesn't work, please provide us with a more comprehensive sample of the code, which shows the TreeView and model setup.

    Regards,
    Dimiter Topalov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready