Design Issue - Creating aspx pages dynamically - c#

I currently have a web application that serves a single dashboard page. The client would like multiple dashboards for different systems displayed on the same physical machine. They would like to avoid installing many instances of the application as this would require opening up too many ports to view different dashboards.
They have suggested I alter my application to include multiple tabs/pages for each dashboard. Currently I have some C# classes which gather and process the data and feed it into the Default.aspx.cs file which then uses Web HTML controls to generate the html content and feed it into Default.aspx page via a placeholder.
At runtime I don't know how many Dashboards there will be or what they will be called (the C# classes read information from textfiles located in each dashboard directory on the drive, C:\Dashboard1*.txt C:\Dashboard2*.txt).
I am new to this and I'm struggling to see how to split the design out so that I can dynamically create the aspx.cs file and aspx file for each dashboard object.
Would a solution be to create a Dashboard.cs class that handles all the html generation (in place of default.aspx.cs) and creates an aspx page dynamically for each dashboard found on C:. Would all of the Web HTMLControls still work without a link between an aspx.cs file and a aspx file?
I realise this question is vague...more specifically I guess I am asking:
How do I create an aspx page dynamically from a C# class and then add web html controls to it? E.g. create the aspx page from a template and then use controls like HTMLGenericControl RowDiv = new HTMLGenericControl('div'). I wouldn't know how to link my C# class to the newly created aspx page so that the HTML Web controls knew they were supposed to be added to that new aspx page.

I would create the dashboard as an user control, and add the controls you need dynamically. Doing it like this you also have the option to create different looking dashboards (if you need it).

Related

Implementing an html/css framework with a master page in visual studio

I'm working on a website for a school project and I designed it in photoshop with 9 columns.
To make life easier I want to use the 960 grid system (Custom for 9 columns) in order to build the design in html/css.
Part of the projects requirement is that I need to use a master page and web forms in c# however I'm not sure how to implement the 960 framework within that.
does anyone have any experience with this?
It sounds like you're being forced to use ASP.Net Web forms. I'd typically recommend you use ASP.Net MVC instead, and it does support Master pages, but really everything is a view, and I expect you would get push-back from the professor. So, to keep with the standard ASP.Net Web forms approach, you should create a new ASP.Net WebSite, and check off that you want a master page. Then Visual Studio will create the Master page for you, and any new aspx pages you create you can click a box to have them inherit from the master page.
If you look at the .Master page, you'll see the same HTML you would see on any site, including the , and tags. You should just include the .css file that contains your 960 framework file, just like you would on any Web page. Then the rest f the page will use the 960 grid, and you can put the necessary col# classes inside the class tag (or CssClass for ASP.Net controls) to get the display to work correctly.

Custom Server Control in ASP.NET and C# -

I've got a aspx and aspx.cs file with some components. Now I want to reuse parts of that page in another page. My approach would be to pull out the duplicate part into a WebServerControl.
So before I waste more time yahoogling, is that even the right idea and if so, is there a way to use parts of the aspx file rather than doing it tediously in RenderContents with the HtmlTextWriter, WriteBeginTag, WriteAttribute and so on. That looks like a mess for complicated layout and sizeable amounts of controls.
What's the standard?
Depends.
The main driving factor is that if you need to reuse your control in multiple web applications, you should go with a Custom Control (.cs in C#).
Else, if you only intend to reuse your control in one web application, choose a User Control (.ascx).
This MSDN article is a good starting point.
UPDATE (since OP asked further details):
To embed JavaScript for a custom control, a common approach is
var initializeScript = string.Format("MyNamespace.initialize('{0}', {1});", ClientID, myScriptString);
Attributes.Add("onmouseover", initializeScript);
Suggest to write JavaScript code in a js file and not in .cs since the latter is a nightmare to maintain and debug. Hope this helps.
It sounds like what you want to do is bundle the items into a User Control. This will allow you to design the control by using existing .NET controls rather than rendering everything out from scratch.
All you need is to create an ASP.NET Web User Control
Taken from MSDN:
An ASP.NET Web user control is similar to a complete ASP.NET Web page
(.aspx file), with both a user interface page and code. You create the
user control in much the same way you create an ASP.NET page and then
add the markup and child controls that you need. A user control can
include code to manipulate its contents like a page can, including
performing tasks such as data binding.

Loading html page from a template on the computer

I am working with a C# on visual studios.
What I would like to know is if it is possible to have a html template loaded from a directory on my computer. Then every time I load up my code it will load up the .html template automatically.
Yes there is a way, not suggested but exist. You can use this code to include the file, inside your asp.net page.
<!--#include file="template.htm"-->
What is dose is that is load this htm file and show it on the place that you have place this declaration. Also you must know that this file can not contain anything dynamically. This is a command code that comes from the old asp script code, and still work on asp.net
So I can not call it as template. If you wish to make something like a real template you must use a master page. Using a master page is far better, and you just copy paste your htm code inside the master page and there you have it.
relative: http://triaslama.wordpress.com/2008/11/19/how-to-include-file-in-aspnet-pages/

How create dynamic view page and css file in mvc3

I am working on small project Fast Food Ads in Mvc3 using c# .I want to create page where user can set the layout of page like header , footer and menu bar etc. manually so I think this page its HTML and Css file should be create dynamically. page name and css file will be unique for every user how can i do this?
I don't want to use any CMS...
In principal you can use the Razor engine in MVC3 to process any of your files. You can already do that for your HTML files.
If you want to use Razor to dynamically process (or even create, on the fly) your CSS, have a look at RazorJs. RazorJs allows Razor to pre-process Javascript files before they are included in a view. I have not looked at it's source code, but I imagine it should be straightforward to adapt it to pre-process CSS as well.

How do you return an embedded resource .aspx page via a httphandler?

Pop quiz hot shots...
I have a Visual Studio 2010 .NET 4 solution with 2 projects, The first project is a c# class library that contains a httphandler and a .aspx page. The .aspx page's build action has been set to "Embedded Resource".
The second project is an asp.net web application which references the first. The httphandler is wired up in the web.config.
I want the httphandler to serve the embedded .aspx page. How do I do this?
Thanks,
James
maybe this is relevant: http://www.west-wind.com/weblog/posts/2007/Jul/23/Loading-an-ASPNET-Page-Class-dynamically-in-an-HttpHandler
.aspx is just a specialized kind of HttpHandler in .NET. Don't forget that.
Thus, .aspx files (ASP.NET Web Pages) actually have implemented IHttpHandler and they have ProccessRequest method. There are two ways to do this:
Based on dynamic compilation nature of Web Forms and markup vs. code-behind, if you want the markup of the page to be compiled dynamically and be executed, you have to extract the page (through code) and save it on the disk. This extraction process can take place on Applciation_Start event.
If you don't like the extraction method, don't forget that you can remove markup entirely and do everything in code-behind (just like PHP or old ASP or ASP.NET MVC). Also remember that your page is actually a class from the point of OOP. Thus simply instantiate it in your HttpHandler and call it's ProcessRequest method, passing the current HttpContext into it.
The way I would do it is through a VirtualPathProvider, not a handler. You can set up and register a virtual path provider to serve pages from an embedded resource (or database, web service, or anything else you can think of).
http://support.microsoft.com/kb/910441

Categories

Resources