问题
No doubt I'm missing something very obvious here - but I'm new to sharepoint so please bear with me.
I've successfully added a master page, created a content type and have created an aspx page for my custom content type - but I can't find its cs file!?
Is there another way in which code-behind is implemented in sharepoint solutions? It seems to be similar when creating controls. I'm used to standard asp .net c# web application development were all of the aspx pages come with the code-behind files 'as standard.'
Thanks.
回答1:
In SharePoint, you can have a code-behind class, but it needs to be in an assembly, not in an aspx.cs code-behind file.
Using Visual Studio 2010, the Page directive for your aspx page would look something like this:
<%@ Page
Language="C#"
DynamicMasterPageFile="~masterurl/default.master"
Inherits="MyNamspace.MyPage, $SharePoint.Project.AssemblyFullName$"
%>
You can then create a MyNamspace.MyPage
class and do everything you would do in a regular code-behind file.
If you take a look at the out of the box aspx pages in the 14 hive\TEMPLATE\LAYOUTS directory, you will see, while some pages have inline code (which is allowed in layouts), almost every page inherits from a class in one of the SharePoint assemblies.
回答2:
Code-Behind is hidden by design from both 2007 and 2010 versions. See here to enable it on a per location basis (PageParserPath).
You'll be better off in the long run in terms of supportability, maintenance and management to stick to convention in SharePoint and use controls rather than code-behind if you can.
回答3:
you should open your sharepoint .aspx
page in sharepoint designer and add inherit statements with your .cs class.
something like :
<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" Inherits="Namespace.ClassName,Namespace,Version=1.0.0.0, Culture=neutral, PublicKeyToken=tokenid" %>
one thing is remain as earlier @Paul's Answer you should enable it on a per location basis (PageParserPath).
回答4:
I advise you to ask this in the SharePoint dedicated site. You can find it here.
But answering you question, there is no code behind available for you in SharePoint pages, at least in MOSS 2007. Not sure about SharePoint 2010 exactly, but must be the same.
来源:https://stackoverflow.com/questions/10206756/wheres-the-code-behind-for-sharepoint-aspx-pages