Add general C# code to a SharePoint 2010 site page - c#

I want to add a C# class to an existing SharePoint 2010 site page. The site page, being created either through the SharePoint UI or in SharePoint Designer, has a web part on it that I want to access and edit its properties. I could just make an invisible web part in Visual Studio, deploy it on the page, and run the code I want to run that way, but I feel like that may be bad practice (or is it)?
I'm kind of talking about how a general non-SharePoint .aspx page can reference a C# file, kind of like below:
<%# Page language="C#" Inherits="System.Web.UI.Page" CodeFile="Example.cs" %>
Would a line like above be added to the .aspx file? How is the .cs file actually deployed on the SharePoint site? Finally, is this even possible?
I'm new to SharePoint and to this site, so I appreciate all help!

By default, SharePoint does not support code behind files in regular pages for security reasons (although, as alfonso mentioned, inline code and code behind files are supported by application pages). You can override this setting in the web.config, but that is not considered to be a good practice.
You certainly could create another web part to do what you want. I have done something similar to that before. But typically, when I want to change the behavior of a custom page, I change the Inherits attribute of the Page directive:
<%# Page
language="C#"
DynamicMasterPageFile="~masterurl/default.master"
Inherits="MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
Create your class, have it inherit from System.Web.UI.Page, then deploy it to the GAC. Not only is this supported, but this is how most of the out of the box SharePoint pages do it.

I think there's no "correct" way to do this with a site page. The only way would be using webparts as you said, or user controls. However, you can add code very easily to an application page. This guy explains this very well.

You should have a look at the developer modules for SharePoint on MSDN
http://www.mssharepointdeveloper.com
There are various ways how to customize SharePoint 2010 as already mentioned earlier here you could use
WebParts
ApplicationPages or LayoutsPages
Custom MasterPages
DelegateControls
There are more ways how to extend SharePoint, so it really depends on your situation or on the requirement you've to achieve.

Related

ASPX website HTML Edit

Good morning,
I have a website that I have to edit for a client, but the website is on a local web server that is designed in aspx. I am looking at all the files and when trying to change Form options I am unable to find the HTML code behind the runat=server
what i ultimately need to accomplish is to change the options in a dropdown menu that allows users to choose from.
I am sorry if i am unable to explain it in depth, my web programming is weak when dealing with this.
Once published, an asp.net site would compile the code behind into a dll, you cannot see the code, only the markup.
Solution is to edit your website in visual studio and republish.

How to reuse web user controls from another project in the solution?

I want to use web user controls inside 'Controls' project inside the 'Web' project in the same solution. but when I reference 'Web' to 'Controls' and then drag the control into a web form in 'web' project the Register directive's src is like this:
<%# Register src="http://localhost:59388/UC/Menu/MenuManager.ascx" tagname="MenuManager" tagprefix="uc1" %>
and an error says:
http:/localhost:59388/UC/Menu/MenuManager.ascx' is not a valid virtual
path.
Some people say thay It's not possible to reuse web user controls and we have to convert them to Server controls, but I've seen somewhere some one did this.
Do you know how to do this?
You cannot use an ascx via http. Essentially you are trying to get the ASP.NET compiled and processed output of the user control if you do that.
You are better off writing a Control Library if you plan to share controls. See this question
Scott Guthrie has a write-up about creating User Control Libraries

Run a aspx page into a Sharepoint folder

I have a aspx page and I'd like to run it into a sharepoint folder. Is there a way to execute this page like a html page? What I really need is to run a c# code that is together the aspx page (code behind) to read a SQLite database and shows the result in a good interface (html-css-javascript).
Obs.: I have a assembly reference for the SQLite.
Thanks a lot!
You need to create an application page inside sharepoint and move your code in that application page. its very simple, please see this link.
http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-create-custom-sharepoint-2010-application-page-using-visual-studio-2010/
Please note that application page only support web forms and not MVC
Not sure "run it into a sharepoint folder" means... You can't have page with code behind in regular SharePoint folders.
You can put ASPX pages (even with codebehind) in Layouts folder on server's disk and they will be avaiable with ....\layouts\your_page.aspx urls.

when do you need .ascx files and how would you use them?

When building a website, when would it be a good idea to use .ascx files? What exactly is the .ascx and what is it used for? Examples would help a lot thanks!
It's an extension for the User Controls you have in your project.
A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.
Simply, if you want to have some functionality that will be used on many pages in your project then you should create a User control or Composite control and use it in your pages. It just helps you to keep the same functionality and code in one place. And it makes it reusable.
We basically use user controls when we have to use similar functionality on different locations of an app. Like we use master pages for consistent look and feel of app, similarly to avoid repeating the same functionality and UI all over the app, we use usercontrols. There might me much more usage too, but I know this one only...
For example, let's say your site has 4 levels of users and for each user there are different pages under different directories with different access mechanisms. Say you are requesting address info for all users, then creating address fields like Street, City, State, Zip, etc on each page. That would be a repetitive job. Instead you can create it as an ascx file (ext for user control) and in this control put the necessary UI and business code for add/update/delete/select the address role wise and then simply reference it all required page.
So, thought user controls, one can avoid code repetition for each role and UI creation for each role.
Ascx-files are called User Controls and are meant for reusability and also for making complex aspx-pages less complex (lift out some part of the page). They could also be beneficial for something called donut caching, that is when you would like to cache a certain part of a page.
If you have a block of code+html that appears on several pages and is sort of independent of that page (say a block of latest news items), you could copy/paste the code to every page.
It is however better to put that code in its own block and just include that block on every page that needs it. That "block" is an ascx file.
One more use of .ascx files is, they can be used for Partial Page caching in ASP.NET pages. What we have to do is to create an ascx file and then move the controls or portion of the page we need to cache into that control. Then add the #OutputCache directive in the ascx control and it will be cached separately from the parent page. It is used when you don't want to cache the whole page but only a specific portion of the page.
ASCX files are server-side Web application framework designed for Web development to produce dynamic Web pages.They like DLL codes but you can use there's TAGS
You can write them once and use them in any places in your ASP pages.If you have a file named "Controll.ascx" then its code will named "Controll.ascx.cs".
You can embed it in a ASP page to use it:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Controll.ascx.cs"%>
When you are building a basic asp.net website using webcontrols is a good idea when you want to be able to use your controls at more then one location in your website.
Separating code from the layout ascx files will be holding the controls that are used to display the layout, the cs files that belong to the ascx files will be holding the code that fills those controls.
For some basic understanding of usercontrols you can try this website

Using Admin Panel How we can create a aspx page in sitefinity?

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

Categories