I am trying to build a C# .NET website that includes files based on DB input.
In PHP, this is simple, where I could do this very easily like this:
<?php include('inc/'.$filename);?>
Is there something similar to do this in C#? I've been trying to figure this out for a while, and can't find a simple solution. I know I can specify static files like this:
<%# Register Src="~/controls/blah.ascx" TagPrefix="test" TagName="blah" %>
... But I can't change the "blah.ascx" dynamically in the code-behind. This would work just fine if I always knew exactly what needed to be included.
Has anyone been able to accomplish this? Thanks!
You can easily add and read from files on the server. Not sure if you want to include existing files or make them on the fly.
Anyway, here's a link that explains how to read from a file on the server.
http://msdn.microsoft.com/en-us/library/94223t4d.aspx
Here is a link that explains how to load user controls dynamically.
http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
I finally found a solution that allows me to include any user control without having to register it in the .aspx page.
// Include external files here
// CustomControl variable can equal something like "home.ascx"
UserControl uc = (UserControl)Page.LoadControl("~/pages/" + CustomControl);
// PageCustomControl can be a Panel or PlaceHolder
PageCustomControl.Controls.Add(uc);
Based on code from http://forums.asp.net/t/1186225.aspx?Add+Web+User+Control+from+Code+Behind
Related
i have a page named Default.aspx and inside it i use an include virtual for the header. Is there a way i can use the controls inside that header in Default.aspx.cs? (Code Behind).It just doesn't recognize the code inside the include file, everytime i try to compile .net throws me a lot of errors related to unrecognized controls..
I know i can put my header inside the default page as usual, but i'd like to keep it in a separate file because it has a lot of content.
Thanks in advance..
Why would you want to do that?
Only reason I can come up with is reusability - and that could be solved by using a masterpage containing the header information or a usercontrol (or even a custom control)
I'd probably go with the masterpage solution - but your description of what and why you want to do as you do is a lite vague...
In ASP.NET MVC I can typically override view e.g. by putting a view with the same name in the DisplayTemplates folder. If I wanted to override the way images are rendered, I could put something like Images.cshtml in the folder.
Now, I want to override the way xforms are rendered in EPiServer. I know how to do this in ASP.NET MVC, but this project uses Webforms.
I have tried to search, but the documentation seems sparse on the subject. In ASP.NET MVC, I could e.g. extend the search engine to search specific locations to look for my views, or put them where ASP.NET looks by default.
This doesn't seem to work in Web Forms. Does anyone know how?
EDIT: EPiServer has an .ascx file which it uses to render an XForm with. I want to tell ASP.NET to use my .ascx file instead. To do this I need to tell ASP.NET to look for my .ascx file, e.g. by telling the ASP.NET view engine to look for my .ascx in a specific folder, or by placing it somewhere the view engine looks by default.
How do I do this?
If you want to replace it everywhere just replace the ascx file. Otherwise I am afraid the answer is no. Web Forms does not look for alternative locations for files by default as controls are usually specified using the full path or the class name. What you want to do would be equivalent of C# looking for alternative namespaces when it cannot find a class name. There are ways to achieve this behavior in Web Forms like for example Dynamic Data but ascx controls is not this.
If you are using the Property web control to display the value you can create your own custom PropertyControl and register it for your type in the PropertyControlFactory. This way you can control how your property will be rendered.
While this doesn't allow you to point out your .ascx directly, you can load it in your server control if you prefer that.
For code examples and a great summary of this (and some other) ways of customizing property rendering in EPiServer, see Mathias Kunto's blog post at http://blog.mathiaskunto.com/2012/03/05/being-friends-with-the-propertycontrolclassfactory-or-101-ways-to-change-episerver-built-in-property-appearances/.
[Final EDIT] Here is a link to the code I wrote in case it helps anyone.
I think I have a solution. Umbraco uses asp.net files for their popups
which is something I haven't used yet but I think I can get the hang
of it. I don't know how to access the aspx from within my class,
should I make a code behind partial class?
Thanks for any help.
I am developing a multi-lingual site, using Umbraco, where content nodes are automatically copied to each language as they are created. Is there any way to implement a custom popup to confirm that it should be copied to all instead?
This wouldn't actually be on the site, rather in the back office.
Or is it possible to open a browser popup with c# as all I really need is a bool value from a message box?
[EDIT: added possible solution]
I sorted this by adapting Umbraco's own create function. I made a new .aspx file and added the functionality that I needed to the code behind.
I was able to add a context menu item that allowed me to call my new page and from there called a method to duplicate the content.
From the method, I pass the new node and get the parent id. Then I compare all the node names for those that match and use the umbraco document.copy() method to recreate the content under each language at the correct position.
If I can make the code more generic then I will upload it as a package to Umbraco.
How can I insert code into a page in ASP.Net from a seperate source file?
Say I have:
<%
Response.Write("hello world");
%>
How can I make it something like:
<% include(helloworld.cs) %>
I know how it work sin the header with the <%# and CodeFile= but I can't make it work for different spots of code. Is there a way ASP.Net handles this? I've tried googling but not sure what to search for.
Another option not mentioned yet is to use ASP.NET Master Pages. This is useful to have a consistent look and feel (and code behind) in the master page which extends to the child pages.
you could create a class library and add a reference to the library.
link that may help:
http://support.microsoft.com/kb/306575
You can use old-style Server-Side include tags like so:
<!-- #include virtual="/inc.inc" -->
But I do not recommend it. You should use User Controls instead; they give you more capability, and do not potentially expose server-side code if someone should happen to try to request them directly.
If you nevertheless decide to use includes like that, note that the content of the file is included in the ASPX/ASCX source code just as if you had typed it right in the main source file itself.
You can't shouldn't.
Instead, you should create a User Control.
how we can create the dynamic page ex. help.aspx and write the code in sitefinity. Because i facing a problem I create a page but i unable to know in which directory the page is lived. If any one help me Suggest.
http://abc.com/sitefinity/admin/pages.aspx
I'm not exactly sure I understand your question correctly. When you create a page in Sitefinity, it doesn't create an ASPX file. The data for the page is kept in the database and served from there. There are no physical files involved.
If you want to write some code that executes when a page loads, you have two options:
Put the code into a control and drop the control on a page created from within Sitefinity
Create a regular ASPX page from Visual Studio and include it as an external page in Sitefinity.
I would recommend the first option, as this would provide you with all the Sitefinity goodness that all pages use - templates, editing through the browser, etc.
If you wanted something else and I misunderstood, please be more specific.
Slavo, The Sitefinity Team