67

I have an ASP.Net page with a C# code behind.

However, I've been asked to not use a code behind - so that it will be easier to deploy in SharePoint.

Is there a way to include the C# code in the ASP.Net page, without using a separate code behind file?

6 Answers 6

98

By default Sharepoint does not allow server-side code to be executed in ASPX files. See this for how to resolve that.

However, I would raise that having a code-behind is not necessarily difficult to deploy in Sharepoint (we do it extensively) - just compile your code-behind classes into an assembly and deploy it using a solution.

If still no, you can include all the code you'd normally place in a codebehind like so:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
  //hello, world!
}
</script>
6
  • 1
    Thanks :) This is the first time we've really had to use Sharepoint here, I wasn't sure if that was actually correct.
    – Sophia
    Feb 19, 2009 at 1:40
  • 9
    I want to re-emphasize Rex M's point that you should seriously consider using code behind assemblies and deploy them through solution files rather than loosening up SharePoints' security for the case of easier deployment. This is Best Practice and is actually rather simple with solution files.
    – webwires
    Feb 23, 2009 at 16:36
  • @RexM Is there a possibility to develop ASP.NET project without using c#? Dec 30, 2014 at 11:59
  • @FaizanRabbani you can use VB.NET, but I cannot fathom why you'd want to. Technically you could use any language that compiles down to MSIL, but you're going a little "outside the box" if you use something other than c# or VB.
    – Rex M
    Dec 30, 2014 at 13:36
  • Hmm I have this concept (I don't know how stupid it may sound) that by we can create projects only by using client-side languages. Dec 30, 2014 at 14:18
30

You can actually have all the code in the aspx page. As explained here.

Sample from here:

<%@ Language=C# %>
<HTML>
   <script runat="server" language="C#">
   void MyButton_OnClick(Object sender, EventArgs e)
   {
      MyLabel.Text = MyTextbox.Text.ToString();
   }
   </script>
   <body>
      <form id="MyForm" runat="server">
         <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
         <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
         <asp:label id="MyLabel" runat="server"></asp:label>
      </form>
   </body>
</HTML>
11

yes on your aspx page include a script tag with runat=server

<script language="c#" runat="server">

public void Page_Load(object sender, EventArgs e)
{
  // some load code
}
</script>

You can also use classic ASP Syntax

<% if (this.MyTextBox.Visible) { %>
<span>Only show when myTextBox is visible</span>
<% } %>
8

There are two very different types of pages in SharePoint: Application Pages and Site Pages.

If you are going to use your page as an Application Page, you can safely use inline code or code behind in your page, as Application pages live on the file system.

If it's going to be a Site page, you can safely write inline code as long as you have it like that in the initial deployment. However if your site page is going to be customized at some point in the future, the inline code will no longer work because customized site pages live in the database and are executed in asp.net's "no compile" mode.

Bottom line is - you can write aspx pages with inline code. The only problem is with customized Site pages... which will no longer care for your inline code.

6

File: logdate.aspx

<%@ Page Language="c#" %>
<%@ Import namespace="System.IO"%>
<%

StreamWriter tsw = File.AppendText(@Server.MapPath("./test.txt"));
tsw.WriteLine("--------------------------------");
tsw.WriteLine(DateTime.Now.ToString());

tsw.Close();
%>

Done
4

I thought you could deploy just your .aspx page without the .aspx.cs so long as the DLL was in your bin. Part of the issue here is how visual studio .net works with .aspx pages.

Check it out here: Working with Single-File Web Forms Pages in Visual Studio .NET

I know for sure that VS2008 with asp.net MVC RC you don't have code-behind files for your views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.