Tuesday, April 17, 2012

How to change Asp.net TreeView font

I have created all the CSS class and applied, but the tree view is not changing the font size. After sever rounds of try I minutely examined the view source and could file that the CssClass is used in specifying ASP:Treeview elements is being override by

 style="border-style:none;font-size:1em;"
this 1em is finally being added to the "<a" tags within. As a result, no font-size specifics are effective.

Finally finding no other way, I planned to change the final rendering (I have used this in a Visual web part and used in SharePoint )

using Microsoft.Office.Server.Diagnostics;

using Microsoft.SharePoint;

using System.IO;

using System.Text.RegularExpressions;

 

#region Changing the Font Size

        protected override void Render(HtmlTextWriter writer)

        {

            try

            {

                HtmlTextWriter output = new HtmlTextWriter(new StringWriter());

                base.Render(output);

                string outputString = output.InnerWriter.ToString();

                //strip out all of the inline styles that the asp menu adds

                //outputString = Regex.Replace(outputString, "style=\"*\"", string.Empty);

                    if (outputString.Contains("1em;"))

                    {

                        outputString = outputString.Replace("1em;", "10pt;");

                    }

                    //outputString = Regex.Replace(outputString,"style=\"border-style: none; font-size: 1em;\"", "style=\"border-style: none;\"");

                    writer.Write(outputString);

            }

            catch (Exception ex)

            {

                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in Left Menu - Control :" + ex.Message);

                throw new SPException("Check Left Menu or Left Menu String");

            }

        }

        #endregion

 

 

You can use Regex.Replace or String.Replace. If you use Regex.Replace create the regular expression likewise.

If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Ref:

http://msdn.microsoft.com/en-us/library/h4kete56.aspx

http://msdn.microsoft.com/en-us/library/ms476607.aspx

http://oreilly.com/windows/archive/csharp-regular-expressions.html

http://www.dotnetperls.com/regex-replace

http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx

No comments:

Post a Comment