ASP.net page without a code behind
Asked Answered
T

6

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?

Telling answered 19/2, 2009 at 1:29 Comment(0)
T
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>
Terrorize answered 19/2, 2009 at 1:36 Comment(6)
Thanks :) This is the first time we've really had to use Sharepoint here, I wasn't sure if that was actually correct.Telling
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.Shirberg
@RexM Is there a possibility to develop ASP.NET project without using c#?Dasyure
@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.Terrorize
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.Dasyure
@FaizanRabbani this isn't really the right forum to discuss that in detail. Perhaps you should open a new question that deals with your specific challenge.Terrorize
S
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>
Surovy answered 19/2, 2009 at 1:33 Comment(0)
I
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>
<% } %>
Indopacific answered 19/2, 2009 at 1:33 Comment(0)
U
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.

Unriddle answered 19/2, 2009 at 15:47 Comment(0)
C
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
Cacka answered 9/2, 2017 at 1:48 Comment(0)
L
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.

Larentia answered 19/2, 2009 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.