Friday, January 25, 2008

Custom TreeView with a Select (onclick) event

One thing that disturbs me a bit is that the TreeView control does not have an event that is triggered when the user clicks an already selected item (in my case, I need to know about this so that I can refresh data. The SelectedNodeChanged is never fired when you click on an already selected item).

So wondered if it is possible to create my custom implementation of the TreeView and it turned out to be quite easy to do after a quick check in the reflector

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GB.ControlLib {

public class GTreeView : System.Web.UI.WebControls.TreeView
{

private static readonly object TreeViewSelected = new object();

public event EventHandler Select
{
add { base.Events.AddHandler(TreeViewSelected, value); }
remove { base.Events.RemoveHandler(TreeViewSelected, value); }
}

protected virtual void OnSelect(EventArgs e)
{
EventHandler handler = (EventHandler)base.Events[TreeViewSelected];
if (handler != null) {
handler(this, e);
}
}

internal void RaiseOnSelect()
{
this.OnSelect(EventArgs.Empty);
}

protected override void RaisePostBackEvent(string eventArgument)
{
base.RaisePostBackEvent(eventArgument);


if (eventArgument.Length != 0) {
char ch = eventArgument[0];
string val = HttpUtility.HtmlDecode(eventArgument.Substring(1));
// Pathseparator in eventArgument is backslash, so we must temporary set the separator
char oldPathSeparator = PathSeparator;
PathSeparator = '\\';
TreeNode node = FindNode(val);
PathSeparator = oldPathSeparator;

if (ch == 's') {
if (node != null) {
if ((node.SelectAction == TreeNodeSelectAction.Select) || (node.SelectAction == TreeNodeSelectAction.SelectExpand)) {
RaiseOnSelect();
}
}
}
}
}


}


}












Friday, January 18, 2008

RichText control in Sharepoint

Today I needed a RichText editor in a sharepoint webpart, so after some googling and using the reflector I found a class in the Microsoft.SharePoint.WebControls namespace that fits my needs, InputFormTextBox!

The control is not well documented (the same as all other controls in the Microsoft.SharePoint.WebControls namespace) but it's quite easy to use:

InputFormTextBox richTextEditor = new InputFormTextBox();
richTextEditor.RichText = true;
richTextEditor.RichTextMode = SPRichTextMode.FullHtml;
// (Can be set as FullHtml, Compatible or HtmlAsXml

The text entered into the control can be get/set in the Text property.

Set the richTextEditor.MultiLine to true if you want to enable multiline editing.

You can control the height and width in the same way as you use the regular TextBox control from the System.Web.UI.WebControls namespace.

References:
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.inputformtextbox.aspx
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sprichtextmode.aspx

Wednesday, January 16, 2008

How to determine the current database in MSSQL

Today I wanted to know what database I currently was connected to in an SQL-script (since I dynamically set the database name when calling SQLCMD) and found out that there is a simple function for this: db_name()

SELECT DB_NAME()

http://technet.microsoft.com/en-us/library/ms189753.aspx