0
votes

I have created a custom webpart using a Sharepoint InputformTextBox control to enter richtext items.

I have to insert an image at the cursor position in InputformTextBox control's RichTextEditor window.

I have an image button, on click of it, I am trying to insert the image at cursor position in RTE window.But i could not get the current cursor position.

Here is the code,

protected void btnimgnew_Click(object sender, ImageClickEventArgs e) {
string newImage = ""; txtcontent.Text = txtcontent.Text.Insert(indexPosition, newImage.ToString()); }

How to get the cursor position in InputformTextBox control's RichTextEditor window?

Thanks.

1
<SharePoint:InputFormTextBox ID="txtcontent" AllowHyperlink="True" RichText="true" RichTextMode="FullHtml" Rows="20" runat="server" TextMode="MultiLine" ></SharePoint:InputFormTextBox>user2018019
<asp:ImageButton ID="imgbtnnew" runat="server" AlternateText="New" ImageUrl="~/_layouts/1033/IMAGES/NEW.GIF" ToolTip="Click here to add the new icon" Visible="true" OnClick="btnimgnew_Click" />user2018019
protected void btnimgnew_Click(object sender, ImageClickEventArgs e) { string newImage = "<img src='~/_layouts/1033/IMAGES/NEW.GIF' alternatetext='New' />"; txtcontent.Text = txtcontent.Text.Insert(indexPosition, newImage.ToString()); }user2018019

1 Answers

1
votes

Current cursor position is javascript.

var rng = RTE.Cursor.get_range().$3_0;

The above will select the range of text selected. This will insert something after that range (in this case, "something" defined by the variable a):

 SP.UI.UIUtility.insertAfter(a, rng);