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);
Related
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
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
I have a user control which is essentially a main menu.
I can place it into my MasterPage hard-coded, but I don't want that, I want to be able to dynamically place it with the code behind of the MasterPage.
<controls:mainMenu ID='MainMenu1' runat='server' />
So what I am looking to do is something like
if **condition is true ** _
response.write('<controls:mainMenu ID='MainMenu1' runat='server' />')
Of course, I know that won't work, but how would I place the control based on a condition in code behind on the master page?
I'm using VB by the way, not C#
You could do something like this -
Dim myControl As Control = CType(Page.LoadControl("yourcontrol.ascx"), Control)
if **condition is true ** _
Panel1.Controls.Add(myControl)
You'd need to add a Panel or PlaceHolder control to your page to hold your control.
I have an ASP.NET repeater pulling comment data from a database.
In my ItemTemplate I placed some Label server controls bound to the fields (username of poster, date, and post text), but apparently Label does not run the data through HtmlEncode before displaying it.
Is there another control I should use? How should I display HTML-encoded data from a repeater?
What about literal with mode="encode"
<asp:Literal ID="Literal1" runat="server" Mode="Encode" />
You can use the literal control which has a mode property with enumeration Encode,PassThrough,Transform.
This has worked for me:
<%# Server.HtmlDecode(DataBinder.Eval(Container.DataItem, "ItemName")) %>
I'm assuming you want to be able to display the comments in html (with formatting et al).
replace the Label control with an Literal control. They both have the Text property but the control will handle your html content.
<asp:Literal>
I am trying to render a simple hyperlink that links to a named anchor within the page, for example:
scroll to down
<a name="namedAnchor">down</a>
The problem is that when I use an ASP.NET control like asp:HyperLink or HtmlAnchor, the href="#namedAnchor" is rendered as href="controls/#namedAnchor" (where controls is the subdirectory where the user control containing the anchor is). Here is the code for the control, using two types of anchor controls, which both have the same problem:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="TestWebApplication1.controls.Test" %>
HtmlAnchor
<asp:HyperLink NavigateUrl="#namedAnchor" runat="server">HyperLink</asp:HyperLink>
The generated source looks like:
HtmlAnchor
HyperLink
I really just want:
HtmlAnchor
HyperLink
I am using the HtmlAnchor or HyperLink class because I want to make changes to other attributes in the code behind. I do not want to introduce a custom web control for this requirement, as the requirement I'm pursuing is not that important enough to justify abandoning the traditional ASP.NET link controls. It seems like I should be able to use the ASP.NET link controls to generate the desired link.
Instead of using the NavigateUrl property, just use the href property
<asp:HyperLink href="#namedAnchor" runat="server">HyperLink</asp:HyperLink>
To set the HREF property in codebehind:
HyperLink link = new HyperLink();
link.Attributes.Add("href", "#" + doc.DocumentID.ToString());
link.Text = doc.DocumentNumber;
This will give you:
blah blah
Set it as a custom property on the link:
HyperLink link = new HyperLink();
link.Attributes.Add("name", doc.DocumentID.ToString());
link.Text = doc.DocumentNumber;
This will give you:
<a name="111">blah blah</a>
If you must use NavigateUrl Property, which is sometimes necesary then you can use:
hypID.NavigateUrl = HttpContext.Current.Request.Url.AbsoluteUri & "#MyAnchor"