HtmlGenericControl Without Tag - c#

Asp.NET C#.
It is possible to create HtmlGenericControl without tag ? If i write :
HtmlGenericControl o_My_Control = New HtmlGenericControl();
It automatically returns a span element. I need an UserControl because i must add this control into an another usercontrol Control.Add(o_My_Control) and i need to directly append html. I use o_My_Control.innerHTML.
Can you help me to find solution ?

A HtmlGenericControl is a control used to represent an HTML element and thus it has to have a name. You can either provide one or if you don't it uses the default value of "span".
If you have some text you want to put into the page directly then you might want to look at the literalControl ( http://msdn.microsoft.com/en-us/library/system.web.ui.literalcontrol.aspx ). This control is designed to be used for "HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server".
So you could just do
myUserControl.Controls.Add(new LiteralControl(myHTMLstring));
And avoid needing to use the innerHTML at all.

A span element is automatically returned because all server controls are surrounded with span tags. There are several ways to go about removing these tags, but if you are able to write a custom control and then override the "RenderBeginTag" and "RenderEndTag" methods, you can solve the issue that way.
See http://weblogs.asp.net/gunnarpeipman/archive/2009/04/09/removing-span-tags-around-server-control.aspx for info on using this method to solve the problem.
Hope that helps.

Related

Best way to give element custom properties in C# Webform

I have a generic html element like this
<span v-bind:class="{ available: days.timeOne }" data-time="10:00" data-date="{{ days.date }}" class="home__visit-featured-days-item-buttons-time">10:00</span>
Which when it is being rendered, is having the vuejs tags being stripped.
I have encountered this issue before when using basic html elements and even control tags like and my solution was to add them manually in the code behind. I don't like this method as not only is long and tedious, it ties back end logic to the view.
Is there a attribute similar to ClientIDMode that I can use to stop these tags being stripped?
ASP.NET webforms will strip out attributes for server controls (those with runat="server") when attributes contain colon (:) characters because these attributes cannot translate to class properties in the back end. However, non-server controls (i.e. raw markup) should just render as written into the ascx file.
Your example doesn't have a runat="server" attribute so I would expect it to render as written. If, however, it is a server control, could you just use raw markup instead?
If it must be a server control I think your only option is to add your attribute in the code behind as you mention e.g. myControl.Attributes.Add("v-bind:class", "{ available: days.timeOne }");
I suppose you are using the CK Editor for entering the HTML code. I wouldn't recommend that since it's WYSIWYG and not a code editor and does such things as stripping some part of the source. If you can, please move your code to Static text web part or to the layout directly. If you need to have it inside the editable region area, you can specify protected source for the CK Editor to let it know what code not to touch:
https://www.google.com/search?q=ckeditor%20protectedsource&rct=j

ASP.NET Adding ContentPlaceHolder to MasterPage

I'm building a kind of simple In-Application CMS and I'm gonno let the user to upload an HTML and a CMS file as Masterpage.
A solution I thought was asking user to put some Pre-Defined Tags inside HTML so I can replace them with ContentPlaceHolder control. The reason of replacing ContentPlaceHolder is that I may have some Web Controls and need postback handling so I cannot convert everything to Html and put them to an HTML and push to client.
And the Questions:
1- Is there any better solution?
2- Is it possible? How can I replace some string with CPH Control?
Regards
To my knowledge you cannot dynamically generate content placeholders at runtime. So your actual masterpage will have to have them in place ahead of time.
I think you may be able to achieve what you are after by putting Literal controls before and after each content placeholder. Then you could parse your CMS html to determine which html comes before a placeholder and which comes after. Finally set each Literal's text property to the respective parsed html.

Implementing paging in Sitecore content pages

I have a section on my website where I plan to add a lot of text-based content, and rather than display this all at once it would be nice if I could add paging on just these pages. If possible, I would like to put all of my content within one content item and have the paging work automatically, building a URL along the lines of http://example.org/articles/title?page=2 or similar.
I've stumbled across an article that mentions paging with Sitecore items and this seems rather close to what I require, although mine requires pagination on a single content item, rather than multiple items. Can someone help me adapt this article towards my needs (if it's on the right track of where I should be looking)?
Is it possible to do this with a Sitecore content item?
http://briancaos.wordpress.com/2010/09/10/create-a-google-style-paging-component-in-c/
I think you'd either want to create your own WebControl and define a custom Render() method that reads the query string to write out the correct information, or you could even do it all in a Sublayout (a user control ASCX file). I've done this before by adding in a custom tag in the Rich text editor via Sitecore (I think I used <hr class="page-break" />) then in my ASCX I'd look for that HTML tag and split the content into chunks from that. I think my solution also used jQuery for some of it but you could probably do it with C# too.
Edit:
You'd want to split the tasks up and have the "paged" content as well as a list of pages (like the article you referenced) so you can easily generate the page buttons. Both of these could be done in two separate repeaters.
You can split the text from a single field into the different pages using approach described here: Split html string to page. All you need to do after that - read the query string and display appropriate block.
If I understand you correctly you have an Item in Sitecore that has x number of text fields and you only want a subset of those displayed depending on input in the querystring ?
In it's simplest form you want a sublayout that handles that.
Basically I'd imagine you having fields called Text1, Text2, text3 etc.
This .ascx could then retrieve the data for fields the fields you'd want using the control and adding them.
Then you could use the code from the article to generate the paging links.
This should be simple enough, but I'd say it would be a better idea to have an item in sitecore and use it's children as the data you want viewed and paged.
It's nicer because if you start out with 5 "page" fields and suddenly want 10, your item will keep on growing, where children can be added without bloating the parent page. Plus the user could then order the children how he sees fit.
I hope this helps a bit.

Content within a user control

I am almost too embarrassed to ask this question, but here we go...
I am not an expert with user controls, and need some design advise regarding trying to achieve a specific desired functionality.
The goal is to have a usercontrol that would render as a complex structure of html elements and css to form an elegant container box. The problem lies in how to populate the contents of the box as each instance of the usercontrol would have its own individual HTML content. The content container div of the usercontrol would be nested deep within the structure of the rendered html. It is undesirable to programmatically set the contents of the usercontrol, or use properties.
In psuedo-code, the desired syntax would be something like:
<usercontrol Title="Some Title"><p>some random html content</p></usercontrol>
A sample of the rendered usercontrol would be:
<div class="CommonBox">
<div class="Title">Some Title</div>
<div class="Content"><p>some random html content</p></div>
</div>
I hope my explanation is adequate. Does this make sense to anyone or is the desired functionality unachievable?
Cheers!
EDIT
I attempted the templated user control suggestion in the hopes of coming to a decent solution. I "waybacked" this site and now have a working templated user control. My next question is, how can I programmatically access the controls nested within the template... say there is a textbox control in the "Description" template from the example link and I want to set its value programmatically? Is this possible?
What you're looking for is definitely possible. One solution is to create a templated user control. In the end you can define the contents similar to how your example looks. Something like:
<uc:MyControl Title="Some TItle" runat="server">
<ContentsTemplate>
<p>some random html content</p>
</ContentsTemplate>
</uc:MyControl>
Here's a simple how-to. I've done this in the past with success. There are lots of resources found through Google as well on the topic.
Yes this is very possible. First you can create a custom control as I describe in this post. Properties in the control become attributes like "Title" you have in your example. Then you can extend your control with the technique shown in this post to add child nodes for the other html inputs you will require.
Enjoy!
It looks like you need two properties for your custom server control.
Title property to render first inner div's content,
Content for the second one. If you want to set contents inside your server control, you should use [PersistenceMode(PersistenceMode.InnerDefaultProperty)] attribute for your property, like this :
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Contents
{
get
{
string contents = (string)ViewState["Contents"];
return (contents == null) ? String.Empty : contents;
}
set
{
ViewState["Contents"] = value;
}
}
PersistenceModeAttribute is explained in MSDN like that :
PersistenceModeAttribute Passing the
InnerDefaultProperty parameter
specifies that a visual designer
should persist the property to which
the attribute is applied as an inner
default property. This means that a
visual designer persists the property
within the control's tags. The
attribute can be applied to only one
property, because only one property
can be persisted within the control's
tags. The property value is not
wrapped in a special tag.
For more information take a look at the last example here.

How do you detect DOCTYPE (Transitional or Strict) server-side in ASP.NET?

I am wondering if there is a way to detect from the server-side which DOCTYPE the page is specified as. I have some HTML and CSS in a custom WebControl that renders differently depending on which DOCTYPE the page is. Is there a Page property or a Response property I could check?
Why don't you have an enum or a boolean on your control that the consuming pages can set (it's not like the doctype should be changing from page to page)?
Thing is, DOCTYPE is a client side declaration and it doesn't take part the control structure of ASP.NET pages (because it exists outside the html element of the page). I concur with #TheCloudlessSky, pass the setting to the control, as the only way I can see you detecting DOCTYPE is by opening up the file itself and reading the first line, this is also pretty useless for compiled web applications too.
Why will the DOCTYPE be changing from page to page?
The DocType will not change from page to page, but you do sometimes need access to it. I am scouring the internet at the moment trying to get at this data.
I know it is there in first HTMLElement (DomElement.data) because it is properly listed in debug mode, but using something like -
dElementList[str].DomElement.data;
This seems to be the only place where the data I need to get at exists, the data inside
Is not posible. I suspect there is a cast needed to a specific element type, but I have not yet come across it. The MSDN example for DomElement does not access any of the elements, but their code is basically the same as mine, dElement is a Dictionary, str is the ID or tag of the element (in this case '!' from it's tag), the Dictionary returns a HTMLElement.
So does anyone know how we get at the at DomElement.data ?

Categories