How to set HtmlEditorExtender's content server-side - asp.net

I'm using the AjaxControlToolkit's HtmlEditorExtender in my ASP.NET 4.0 web app:
<asp:TextBox ID="myTxt" runat="server" TextMode="MultiLine" Height="80px" Width="100%" />
<act:HtmlEditorExtender ID="heMyTxt" runat="server" TargetControlID="myTxt">
<Toolbar>
etc...
</Toolbar>
</act:HtmlEditorExtender>
When I set the content of the text box server-side like this:
myTxt.Text = htmlStringFromDatabase;
...the content in the textbox is the literal HTML markup (i.e. <b>Bold</b> shows up just like that, not like Bold). The formatting doesn't transfer, but the Extender does do its work on the textbox and set up its toolbar and buttons, etc. Is there a different way to set the content?
EDIT: turns out the HTML I get out of myTxt (the control that the extender is attached to) is encoded HTML. So now the question is how to stop the control from encoding its content. This problem is also presented in this question, but I'm not using LoadControl() or the designer to my page; I've written my markup manually.
Also, I don't know if this makes a difference, but I'm pulling the text out of the TextBox in the page's Page_Load handler.

Try to do like this,
myTxt.Text = HttpUtility.HtmlDecode(htmlStringFromDatabase);

I was able to solve this problem like this :
Literal lit = new Literal();
lit.Mode = LiteralMode.PassThrough;
lit.Text = HttpUtility.HtmlDecode(HTMLTExt);
TextBox1.Text = lit.Text; // The text box which HTMLEditorExtender is attached to

Related

Updatepanels not working when filling the CKEditor / Tinymce from the Database

I am developing website by using ASP.NET and I am using CKEditor as a richtextbox. In there everythings works fine.
I am using this code to achieve it
<asp:TextBox ID="txtDescription" runat="server" Width="100%" TextMode="MultiLine" Rows="15" AutoComplete="off" ClientIDMode="Static" MaxLength="6000"></asp:TextBox><br />
<script src="//cdn.ckeditor.com/4.5.3/basic/ckeditor.js"></script>
<script type="text/javascript">
window.onload = function () {
CKEDITOR.replace('txtJobDescription');
CKEDITOR.config.htmlEncodeOutput = true;
};
</script>
So everythings works fine.When I save the content of the textbox I am using
HttpUtility.HtmlDecode(txtDescription.Text)
to do it.
Probelem is when I read the saved text from DB and assigned it to Ckeditor text box all the updatepanels in that page are not working.(Which are working properly)
To assign it I am using this code.
DataTable dt=new DataTable
dt = objBLL.Get();
txtName=Text= dt.Rows[0]["name"].ToString();
txtDescription.Text = dt.Rows[0]["description"].ToString();
If I comment the last statement all the updatepanels are working properly.
I tried this with tinymce also. Probelem is same.
Whats wrong with my code?
I am answering to my own question.
You can either set page ValidateRequest=off at the page level. But this will make other textboxes also post HTML content.
So to avoid that use ValidateRequestMode=disabled in specific control.
This will allow only the selected control posting the HTML tags to server.

How to Display as Html content in Textbox

How to Display Html content in Textbox Control
Eg:
string text="<b>" + Hi How are you + "</b>";
txtEditor.Text=text.ToString();
I have to display those text in bold Letters, How do i achieve this
You can change the font property of the TextBox:
txtEditor.Font = new Font(txtEditor.Font, FontStyle.Bold);
See this link for an existing html editor. I strongly recommend that you carefully read the paragraph about Cross Site Scripting, whether you decide to use that control or not.
To display text bold in asp:textbox control, you can use style at design time or runtime
in .aspx page Design time
<asp:TextBox ID="textBox1" style="font-weight:bold;" runat="server"></asp:TextBox>
OR
in Code Behind
textBox1.Font.Bold = true;
You can try any one to get desired result. My suggestion over here is that use CSS style to get more advanced formatting in textbox

How do I convert asp:textbox to freetextbox

I have an asp:TextBox that I need to convert to a FreeTextBox. The following is my asp:TextBox aspx code:
<%# Register TagPrefix="FTB" Namespace="FreeTextBoxControls" Assembly="FreeTextBox" %>
<asp:TextBox ID="tbxRiskDesc" runat="server" Width="90%" TextMode="MultiLine" Rows="6" onblur="Resize(this);" onkeypress="Resize(this);" ></asp:TextBox>
<rad:RadSpell ID="spellRiskDesc" runat="server" Width="5px" ControlToCheck="tbxRiskDesc" WordIgnoreOptions="UPPERCASE" FragmentIgnoreOptions="EmailAddresses,Urls" DictionaryLanguage="en-AU" SupportedLanguages="en-AU,English" AllowAddCustom="true" DictionaryPath="~/RadControls/Spell/TDF/" SpellCheckProvider="EditDistanceProvider" EditDistance="2" ButtonType="ImageButton" />
The following code is in the aspx.cs code:
Page_Load:
tbxRiskDesc.Attributes.Add("onchange", "setDirty();");
Reset_Data:
SetTextCurrentValue(tbxRiskDesc, dtEditTable, "RiskDesc");
Clear_Data:
SetTextValue(tbxRiskDesc, "RISK TITLE & DESC.:");
When I replace the "asp:TextBox" code in the aspx file with the following, the code behind has errors and i dont know how to change the code for the FreeTextBox for the Page_Load, Reset_Data & Clear_Data sections.
<FTB:FreeTextBox ID="tbxRiskDesc" runat="server"></FTB:FreeTextBox>
The reason for the change over is because we need to allow the users to put bullet points and hyperlinks into the text boxes.
All help is appreciated.
You have two options:
recommended: replace the textbox with RadEditor which has built-in integration with RadSpell
use the RadSpell API to spellcheck the FreeTextBox control: You have to get a reference to the body element of the iframe content area of FreeTextBox. After that you can implement a custom text source similarly as it is done for RadEditor in the following forum thread: Problem with spellcheck.
Using the RadEditor's get_contentAreaElement() method you can get a reference to RadEditor's iframe content area element and after that to get a reference to the body tag of the iframe:
var editorSource = $find('RadEditor1').get_contentAreaElement().contentWindow.document.body;
You should see in the FreeTextBox documentation or forums how to get a reference to the body element and update the above line in the provided in the forum article custom text source code.

Replace HTML with PlaceHolder control in ASP.NET

Is there any way to read some HTML and replace that HTML with a PlaceHolder control dynamically at runtime?
For example, I have some HTML that contains tags such as ##MainContent## or ##SideContent##.
I need to somehow find each tag and dynamically add a asp:PlaceHolder control.
EDIT - The reason for this is to read the HTML in and then dynamically add controls to a section of the page. I'm attempting to create a CMS system, although I'm not sure this is the best approach.
I hope this is a thought exercise. :)
Ideally, you should just replace those tags in your markup with an <asp:PlaceHolder> control. If you cannot do that (for whatever reason), try replacing them with an <asp:Literal> control:
<asp:Literal ID="litMainContent" runat="server" Text="##MainContent##"><asp:Literal>
<asp:Literal ID="litSideContent" runat="server" Text="##SideContent##"><asp:Literal>
That way, they will still render on the page exactly as they do now. Then you should be able to use litMainContent and litSideContent in your code-behind:
For VB.Net:
Dim phMainContent As PlaceHolder = New PlaceHolder
Dim phMainContent As PlaceHolder = New PlaceHolder
litMainContent.Controls.Add(phMainContent)
litSideContent.Controls.Add(phSideContent)
For C#.Net
PlaceHolder phMainContent = new PlaceHolder();
PlaceHolder phMainContent = new PlaceHolder();
litMainContent.Controls.Add(phMainContent);
litSideContent.Controls.Add(phSideContent);

Multiline textbox - "Code blocks are not supported in this context"

I have a multiline (> 50 lines) textbox containing plain text. I want to add either a session variable to the text in this box. In asp I would have done it by putting in <% %> code blocks but in .net I am getting the following error: "Code blocks are not supported in this context". I assume therefore that this would need doing in code behind.
Is there a quicker way than putting all the text from the textbox in a string in code-behind and then adding the variable on like this? I would like to keep the text in my aspx page if possible.
Thanks
How about your codebehind does something like:
myTextbox.Text += Session ["mySessionVariable"];
after you've filled the textbox.
Incidentally, you don't have to
'put all the text from the textbox in
a string in codebehind'
as the .Net framework exposes all the front-end controls as codebehind objects automatically.
EDIT:
<asp:TextBox ID="TextBox1" runat="server" Rows="15" TextMode="MultiLine" Columns="70" Text='<%# Session["var1"] %>'></asp:TextBox>
This will work for binding just the session variable to the control. Don't forget to call
Page.DataBind();
after you've set your Session variables. Probably in Page_Load.
This will allow the binding such as it is, to occur. This won't work if you want to mix up static markup text with dynamic variables. For that, you'll need to get busy in the code-behind.
HTH.
Have you tried <%=Session["MySessionKey"] %> ?

Resources