GridView to Excel ASPX in SharePoint - c#

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

Related

In VS 2019 - How to regenerate code-behind and designer files from an existing aspx page

I imported in my project Web c# an aspx page, for example, mypage.aspx, with HTML code. This page is very simple.
All I want is to generate the 2 files (in IDE) aspx.cs and designer.cs (mypage.aspx.cs and mypage.aspx.designer.cs).
I tried "Convert to web project" functionality but it does not work.
Any idea, please?
Of course, I read several solutions on this site but none worked (with vs 2019).
If there is no actual code in the original code-behind file then it's pretty easy to do. Create a new page of the same name. Then just copy and paste the markup from the original, taking care to leave the newly-created page directive as is.
If there was logic in your original code-behind then you're out of luck. You might try decompiling the original DLL and try to re-create the original code that way.

Editing HTML via asp.net

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?

Where Controls Declarations in a website project are put

I have website i cant access the controls in the code-behind
i drag controls and add them to ASPX file and when i try to access them i cant.
i tried opening the website using:
Visual Studio 2012
Visual Studio 2010
when i even double click on a button it creates its event in the code behind but i still cant access the control!
there is no designer.cs file since this is a web site any idea how to fix it ?
If memory serves, the markup is the designer for a page that lacks a designer file. You can observe this behavior in classic ASP pages that have been ported to .NET.
The compiler simply parses the page, looking for a tag that includes runat="server" and generates a server-side control for it. If there's no ID, one is generated for it. Further, if AutoEventWireup="true" is set at the page level, the events will be automatically connected to these controls, as if by magic. It's all just compiler voodoo, if you ask me, but rather convenient compiler voodoo.
But that's an educated guess, based on experience, and I'm sure someone with far more experience with ASP.NET internals can provide more riveting details. (Jon Skeet, et al.)
Make sure your .aspx file specifies its codebehind file:
<%# Page Title="" Language="C#" CodeFile="YourPage.aspx.cs" Inherits="Content_YourPage" %>
If it is a website then websites don't have designer.
But if you need the designer's then try to convert it to web application

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/

Broken link between aspx and aspx.cs files

I've had the same problem a couple of times with different ASPX pages after renaming them and I am surprised that I can't find someone else with the same problem on stackoverflow.
When I run my ASP.NET C# project, the debugger gives me a message like this one.
Error 5 The name 'txtTitle' does not exist in the current context
It seems that the aspx and aspx.cs files at no longer bound. The only fix I have found for this is to recreate the page and copy/paste my code.
Any idea how to fix this without recreating the whole thing?
Thanks
The code file contains a partial class that is referenced in the ASPX header declaration. Both file name and the actual class in the ASPX header have to match for this to work.
<%# Page Title="TestPage" Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
In your case probably the class name does not match. Check if the class name in the codebehind .cs file matches the name after Inherits.
The concept of partial classes used in ASP.NET is detailed here.
I am surprised at this occurring I regularly rename Silverlight user controls with no harmful side-effects.
Are all of the references to classes being renamed in the aspx page and code behind ?
You might try using this util to relink them.
This can also be caused by the whatever.aspx.designer.vb file being dependent upon whatever.aspx.vb instead of whatever.aspx. If this is the case, it should show incorrectly in the file tree when you expand the group of files in Visual Studio. To fix it, simply exclude the affected files from the project and then re-include them - the faulty project config file should be regenerated and it should now work correctly.
(Same theory for C#.)
I've had the same problem and noticed that sometimes, but not always, the CodeFile setting in the .aspx page is not updated when the class is changed. If this is the same problem you have, you can change it manually:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyClass.aspx.cs" Inherits="MyClass" %>
^^^^^^^

Categories

Resources