Editing HTML via asp.net - c#

So I am using asp.net to make a site, and I came across a bit of a problem. I need to edit the html from an .aspx.cs file, and while this normally isn't a problem, the html file is located in a templates folder elsewhere in the project. I have tried using javascript, but I kept getting errors saying "cannot setAttribute/change innerHTML of type null.
Would it be better to continue trying to fix this with javascript, or does asp.net have something for this?
ALSO NOTE: the html is in an html file, not a .aspx file, so asp.net controls (such as runat="server") don't work.
EDIT: What I am trying to do is hide a div that holds a navigation bar for admins if the current user is not an admin.
For javascript fixes, I have tried Page.RegisterStartUpScript and Response.Write (called from the .aspx.cs file), but both give me the error described above. I can't (as far as I know) have a javascript file just hide the div without somehow being called or originated by the c#, as I need to find out whether or not they are an admin

What you need to do is convert the HTML document to an .aspx file and include it (Ref. http://www.mikesdotnetting.com/Article/144/Classic-ASP-Include-Files-in-ASP.NET) in the other documents where it is to be used. Then, you can reference the control in your code behind, using whatever methods are available to you for determining whether or not a user is logged in. For example:
if (Security.IsLoggedIn())
{
myIncludeFile.Visible = true;
}
else
{
myIncludeFile.Visible = false;
}
Does that make sense to you?

Related

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/

GridView to Excel ASPX in SharePoint

I have an ASPX file which I am running in SharePoint that has a GridView which I am attempting to export into an excel spreadsheet. I know the code I have for the export is correct; however, I am getting this error:
RegisterForEventValidation can only be called during Render();
I have done some research and have found a solution, which might work with normal ASPX pages created in VS with a CS code behind class, which is setting the EnableEventValidation to false, and I am getting a Parser error when I attempt to use this solution on my ASPX page.
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"%>
Is there any other way to allow this, or any other workaround to my problem using just the ASPX page without the code behind? All of my C# code is within the head and has to be in order for me to run it in my SharePoint environment. 12 hive storage of the CS class is not an option for me. Also, I am not wanting to change anything in my web.config folder if possible.
Thanks
Try creating it as a Web Part - this is the more common approach for this type of scenario in SharePoint.
In SharePoint, all customized pages are run through the Safe Mode Parser which prohibits inline code. Also, this parser does not allow adding controls to pages which have not been marked as "safe".
To get around your issue, you might want to look at "Application Pages" which are added to the \LAYOUTS directory. They reference the master page and can have inline code, but they can't be customized. They are compiled into a single assembly DLL:
Creating an Application Page in Windows SharePoint Services 3.0

C# Winforms WebBrowser edit html

I want to display a webpage in a WebBroswer in my winforms application. I however want to use some custom css to change how the page looks. Is it possible to attach a style sheet and edit the html page you are viewing?
You can use this web control http://www.codeproject.com/KB/miscctrl/csEXWB.aspx
and save it as a web page and load and you can show the source in another text box , once changes are made you can save to a temp file and load it. There is an option to view local file.
I don't think that kind of thing is supported.. but anyway that would be a horrible thing to do. you may end-up interfering with the CSS used by the client page.
Have you ever seen your browser override your CSS used by page and say i want to show it this way. (I know development tools like firebug,etc do it)

ASP.NET C# Properly Generating HTML for Page

I realize this is probably a fundamental thing I should know but I am self-teaching myself C# and asp.net so I am a little lost at this point.
I right now have 2 pages. One is an .aspx (with aspx.cs file included) that is blank and html is generated for it from a Page_Load function in the cs file. The HTML is very simple and it is just an image and some text.
The second file is a shtml file which has lots of things, serverside includes, editable and noneditable areas. I want to put my webapp into this file. My asp.net app uses Response.Write to just write out the html. This does not flow well with this page as all that does is write it at the top of the page which is because it is ran first and generates it at the top.
How can I make it to where I can generate HTML code inside the page, like within a specific DIV so it does not mess up the page. Where would a starting point be in learning how to do that.
I should note that I do not need any interaction from the user. All of this should generate right away.
I think you need to read up on some basic ASP.Net documentation and tutorials. Response.Write is not the correct approach - you need to understand how the ASP.Net page lifecycle works and how WebControls are used to render the html.
ASP.Net tries to abstract away having to create your html manually for the most part.
So if i have understood the questions correctly.
You already have an existing page/application (the shtml file) that you want to extend with some new ASP.NET components by including output from the ASP.NET page in the existing page?
This is as not something that is out of the box "supported" by ASP.NET and you "won't" be able to execute the aspx page using SSI. But you can do the opposite, an ASP.NET page does support SSI. So if you are not using any other scripts in the shtml file this might be a solution.
Otherwise the only common solutions would be either to use an AJAX framework and let it call the ASP.NET from within the existing pages or to use an iframe solution. In both cases the client will be resposible for making the calls to the ASP.NET pages and merging the results.
And then you have a issue with controlling the output from the ASP.NET page?
The Polymorphic Podcast has a good article on Controlling HTML in ASP.NET WebForms .
You can add a Literal control to the page inside the div:
<div>
<asp:Literal ID="litMarkup" runat=server />
</div>
then in your code-behind:
litMarkup.Text = "<strong>Your markup</strong>";
I don't know how well this would work for you, but could you try using an iframe to house the ASP.NET page? This should keep it in the specified region and not overwriting your shtml file. It may be something to think about.
If it is necessary that you generate your HTML output from C# code, and you would use this in more than one place, I think you may be thinking of something like what are called ASP.NET Custom Controls (not to be confused with "User Controls"-- though you probably could put together a solution with those as well, using a Literal control as another person suggested). The MSDN documentation would be a good starting point. In general, though, the writing-out-HTML-yourself-from-code model (like you would with, say, CGI applications), is not the usual ASP.NET model of development, as it largely defeats the point of using ASP.NET at all. You'd mostly want to do this sort of thing if you are writing your own web control, though this might be exactly what you are doing (hard to tell from the description).

C# code for saving an entire web page? (with images/formatting)

I've been struggling to find an exmample of some C# code (I'm using C# Visual Studio 2008 Express) that can programmatically save an entire web page (given a URL) including the images and formatting (e.g. CSS). The intention is that in a subsequent phase I'd ship this off (not sure how yet) so it could be viewed later via a browser.
Is there an example of the most simple approach (leveraging the .NET Framework methods) to save an entire web page? Saving as one page with a subdirectory for images, or otherwise. Basically the same as what you get with browsers when you say "save entire web page".
The simplest way is probably to add a WebBrowser Control to your application and point it at the page you want to save using the Navigate() method.
Then, when the document has loaded, call the ShowSaveAsDialog method. The user can then save the page as a single file, or a file with images in a subdirectory.
[Update]
Having now noticed "programatically" in your question, the above approach is not ideal as it requires either user involvement or delving into the Windows API to send input using SendKeys or similar.
There is nothing built-in to the .NET Framework that does all of what you ask.
So my approach revised would be:
Use System.NET.HttpWebRequest to get the main HTML document as a string or stream (easy).
Load this into a HTMLAgilityPack document where you can now easily query the document to get lists of all image elements, stylesheet links, etc.
Then make a separate web request for each of these files and save them to a subdirectory.
Finally update all relevent links in the main page to point to the items in the subdirectory.
In effect you would be implementing a very simple web browser. You may run into issues with pages that use JavaScript to dynamically alter or request page content, but for most pages this should give acceptable results.
From code Project: ZetaWebSpider
It's definitely not elegant, but you could navigate a System.Windows.Forms.WebBrowser to the URL and then call its ShowSaveAsDiagog() method to save the page.

Categories

Resources