Visit our SharePoint Forum

SharePoint developer? Submit Yourself as Freelancer

Thursday, February 14, 2008

Add "RichTextBox" in SharePoint WebPart and ASP.Net User Control

Today I had to add a "RichTextBox" in the Sharepoint Web Part,but AS there is no such control in ASP.Net, I was a little worried!.But After "google-ing" for some time,I stoped at a Sharepoint Control named "InputFormTextBox". Using a property named "RichText",this control can behave like an Extended Rich text box of Sharepoint.
InputFormTextBox is a class in "Microsoft.SharePoint.WebControls" namespace.So we will Use its Multiline,RichText and TextMode properties to make it a RichTextBox.

To Use This control in a Sharepoint Webpart Project we Use:

InputFormTextBox _txt = new InputFormTextBox();

The Code below for adding it into Webpart goes below:

using System;using System.Security;
using System.Net;
using System.Collections.Generic;
using System.Text;using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

[assembly:AllowPartiallyTrustedCallers]

namespace TestRichTextBox
{

public class TestRTB : WebPart
{

Button _btn = new Button();
InputFormTextBox _txt = new InputFormTextBox();

}

protected override void CreateChildControls()
{
try{
_txt.TextMode = TextBoxMode.MultiLine;

_txt.Rows = 10;_txt.RichText = true;

_txt.RichTextMode = SPRichTextMode.FullHtml;

this.Controls.Add(_txt);

_btn.Text = "Click me";

_btn.Click += new EventHandler(_btn_Click);

this.Controls.Add(_btn);
}

catch (Exception _ex)
{
Page.Response.Write(_ex.ToString());
}
}

void _btn_Click(object sender, EventArgs e)
{
Page.Response.Write(_txt.Text);
}

protected override void Render(HtmlTextWriter writer)
{
RenderChildren(writer);
}
}
}

Scince I was using ASP.NET User Control and was handlling all the events in it. I needed to add this Control in my ASP.Net .ascx file.(USer Control file).

SO I planned, TO add this InputFomTextBox Control in my WebApplication toolbox.So that i can easily drag and drop it in my form.


To Use InputTextbox Control in ASP.Net User Control follow the Steps Below:

To Get Started:

1. Add Reference to Microsoft.Sharepoint.dll using Add reference option.

Now, To add Sharepoint control in your design tool


-> Right Click on design tool to add new tab.
->Name it as "Sharepoint Tools" and OK.
-> Now right click on the tab and click "Choose Items"
-> Under .Net Framework Components select "InputformTextBox" and click ok

Now drag and Drop the tool in your User control/ASP.Net WebApplication

Use RichText = "true" and Multiline = "true" to make it a RichText Control.


Note: You may find that ASP.Net Automatically Registers the assembly with the strong name on the top of ascx page, when you add the control in toolbox tab.

You Can aslo Use lots of other Sharepoint Controls like Datetime control an checkbox controls and other.

References for this article :

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





8 comments:

Sagar said...

Thanks it really helped...

Anonymous said...

Thank you! The InputFormTextBox is working splendidly for me!

Mohan said...

When I add the InputTextBox to My ascx control, It gives me normal text box type. Not including the Top bar ( font , picture , bold ).

any help?

Vikash said...

Hi ,
i am adding richtextbox control dynamically in to the Page which is acting as a sharepoint page now, but when i declare
InputFormTextBox rch = new InputFormTextBox();
it start throwing error.

Unknown said...

I have added InputTextBox control in my custom web control (Ajax enabled), which works fine for the first time. But not during postback. When the postback happens the InputTextBox becomes text area and doesnt display the toolbar on top. Is there any way i can resolve this issue?

Anonymous said...

Thirumurugan :

I am facing similar situation like yours
any one have any solution on this?

Anonymous said...

Top bar ( font , picture , bold ).
does not appear to textbox...

Anonymous said...

RichTextBox WinForms Control

SharePoint Programming