Creating Dynamic SharePoint Help With Lists And RadToolTipManager

Using the RadToolTipManager to build dynamic help options within custom SharePoint WebParts is a really useful trick. It can greatly increase the usability of an application, and you can dynamically store the different help items in SharePoint fields. In this example I am going to dynamically expose some fields on a custom WebPart through a BaseFieldControl method, and just use the field names as keys to bind the different help items.

So let’s assume I am reflecting on a field to surface the correlating WebControl:

  1. BaseFieldControl webControl = SPField.FieldRenderingControl;
  2. webControl.ListId = SPList.ID;
  3. webControl.ItemId = SPListItem.ID;
  4. webControl.FieldName = SPField.Title;
  5. webControl.ID = string.Format("MyField_{0}", SPField.Title)
  6. webControl.ControlMode = mode;

I am going to use this static helper method for the actual tool tip and target control instantiation:

  1. public static void CreateHelpAndAdviceButton(string fieldName, Control Parent, RadToolTipManager manager, bool show)
  2. {
  3. string buttonId = String.Format("btn{0}", fieldName);
  4. ImageButton btn = new ImageButton();
  5. btn.ImageUrl = "/_layouts/images/help.png";
  6. btn.Visible = show;
  7. char[] charsToRemove = new[] {'\r', '\t', '\n', ' ', ':', '-'};
  8. string[] results = String.Format("{0}", buttonId).Split(charsToRemove);
  9. StringBuilder transformedString = new StringBuilder();
  10. foreach (string s in results)
  11. {
  12. transformedString.Append(s);
  13. }
  14. btn.ID = transformedString.ToString();
  15. manager.TargetControls.Add(btn.ID, fieldName);
  16.  
  17. RadToolTip tip = new RadToolTip();
  18. tip.ShowEvent = ToolTipShowEvent.OnMouseOver;
  19. tip.TargetControlID = buttonId;
  20. tip.IsClientID = true;
  21. tip.ID = String.Format("RadToolTip{0}", buttonId);
  22. tip.HideEvent = ToolTipHideEvent.LeaveToolTip;
  23. Parent.Controls.Add(tip);
  24. Parent.Controls.Add(btn);
  25. }

The RadToolTipManager is assumed to be created, but you can just create one with a static method and pass it in. Here is a method for a pretty basic popup with delay window:

  1. public static RadToolTipManager ConfigureTooltip()
  2. {
  3. RadToolTipManager tooltipManager = new RadToolTipManager();
  4. tooltipManager.Position = ToolTipPosition.TopRight;
  5. tooltipManager.Animation = ToolTipAnimation.Resize;
  6. tooltipManager.RelativeTo = ToolTipRelativeDisplay.Mouse;
  7. tooltipManager.ShowEvent = ToolTipShowEvent.OnMouseOver;
  8. tooltipManager.HideEvent = ToolTipHideEvent.ManualClose;
  9. tooltipManager.ContentScrolling = ToolTipScrolling.Default;
  10. tooltipManager.AjaxUpdate += OnAjaxUpdate;
  11. tooltipManager.EnableShadow = true;
  12. tooltipManager.Width = 200;
  13. tooltipManager.Height = 150;
  14. tooltipManager.ShowDelay = 200;
  15. tooltipManager.HideDelay = 1;
  16. tooltipManager.AnimationDuration = 300;
  17. tooltipManager.MouseTrailing = true;
  18. tooltipManager.ShowCallout = true;
  19. tooltipManager.Modal = false;
  20. return tooltipManager;
  21. }

The OnAjaxUpdate is responsible for the actual display of the item, so that has to be defined. Especially since it’s in the above method! This is also where we will look up to the SPList and get the relevant items. We are just using the field name as a key against the list, and a super simple SPListItem iteration.

  1. public static void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
  2. {
  3. UpdateToolTip(args.Value, args.UpdatePanel);
  4. }
  5. public static void UpdateToolTip(string key, UpdatePanel panel)
  6. {
  7. foreach (SPListItem item in SPContext.Current.Web.Lists["SomeList"].Items)
  8. {
  9. string fieldName = item["Field Name"].ToString();
  10. if (fieldName == key)
  11. {
  12. string content = item["Text"].ToString();
  13. Literal lbl = new Literal();
  14. lbl.Text = content;
  15. panel.ContentTemplateContainer.Controls.Add(lbl);
  16. }
  17. }
  18. }

Then, just call the static helper method for the button generation:

  1. CreateHelpAndAdviceButton(Some Field Name, ControlToAddTo, RadToolTipManager, false);
share save 171 16 Creating Dynamic SharePoint Help With Lists And RadToolTipManager

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>