Preaching SharePoint

Programmatically create RichTextBox in SharePoint WebPart


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



Responses

1 Respones to "Programmatically create RichTextBox in SharePoint WebPart"

Post a Comment

 

Do you want SharePoint 2013 to inherit Metro Style design?

Search This Blog

Return to top of page Copyright © 2010 | Platinum Theme Converted into Blogger Template by HackTutors