locked
Where I place the code behind code? RRS feed

  • Question

  • Hi All,

     

    It seems to me like sometimes I'm pretty stupid (hopefully not always). I'm sure there's a really simple solution to this.

     

    There're a lot of examples on the web dealing with code behind logic e.g. http://www.sharepoint-tips.com/2007/10/another-simple-code-snippet-using-user.html which seems very neat to me. However, I don't know how and where to enter the code!

     

    Call me stupid or not - I read a lot of examples with code behind and feature deployment, but I just have one .aspx site and want to put some code behind this. Nothing more...

     

    My problem is pretty simple: I've got a dropdown list and want to select a default (current) user. That's all. Without Sharepoint it would be a pice of cake for me :-((

     

    I'd appreciate if someone could give me a hint.

     

    Nearshore

     

     

    Monday, December 3, 2007 2:24 PM

Answers

  • There are many advantages to placing your code in a code-behind library, not least of which:

    • It allows you to step through and debug your code
    • You get a "cleaner" development environment .. I find the script-tag inline code approach to be a little buggy
    • You can provide better Intellectual Property protection, because your ASPX file won't expose all your source code
    • You can provide better code protection, because with inline script tags any idiot with NotePad can change the code!

    To use code-behind you place your code in a separate Class Library project.

    The Class Library should be strongly named and placed into the GAC after compilation.

     

    Then, when building your ASPX file, put a reference in the Page header that references your class name and assembly.

    You will need to use the fully qualified assembly reference

    (download and use the .Net Reflector to get this easily, any decent search engine should find a download link quickly enough)

     

    For example .. if you have an Assembly called "MyAssembly" with a public key token of "d3ee81fc11cb7d17"

    And say you have a class called "MyForm".

     

    When you build your custom Application Page (ASPX file) you add the following header:

     

    Code Block

     

    Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="MyAssemly.MyForm, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d3ee81fc11cb7d17"

     

    [EDIT - I've removed the < % and  % > tags because they were causing the text to fail]

     

     

     

    Note that the "Inherits" attribute will reference the fully-qualified classname and THEN the 4-way assembly reference.

     

    When you are building your Class Library you will need to add references to any of the server-side objects that you have referred to in your ASPX file.

     

    For Example:

     

    ASPX File

    Code Block

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <asp:Panel ID="pnlMain" runat="server"/>

    </< FONT>asp:Content>

     

     

    Class Library

    Code Block

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using Microsoft.SharePoint;

    using Microsoft.SharePoint.WebControls;

     

    namespace MyAssembly

    {

     

    public partial class MyForm : Microsoft.SharePoint.WebControls.LayoutsPageBase

    {

    protected Panel pnlMain = new Panel();

     

    protected void Page_Load(object sender, EventArgs e)

    {

    pnlMain.Controls.Add(new LiteralControl("Hello World"));

    }

    }

    }

     

     

    When it renders, your Application Page should now show the text "Hello World".

     

    If you load your Class Library, you should be able to Attach to the W3P.exe running on the SharePoint server and step through your code too!

     

    hope this helps

     

    MKeeper

    Tuesday, December 4, 2007 4:40 PM

All replies

  • From everything I've seen you have to use a server script block (<script language="c#" runat="server"> ... </script>).  At least when you're talking about aspx or master pages that you've uploaded.  See this blog post for web.config changes that you'll need to make to support this: http://weblogs.asp.net/soever/archive/2006/07/27/SharePoint-2007_3A00_-using-ASP.NET-server-side-code-in-your-pages.aspx

     

    Monday, December 3, 2007 5:32 PM
  • Here's some more information: http://msdn2.microsoft.com/en-us/library/bb892187.aspx.  What's it basically saying is that you can have a code-behind with application pages.
    Tuesday, December 4, 2007 4:10 PM
  • There are many advantages to placing your code in a code-behind library, not least of which:

    • It allows you to step through and debug your code
    • You get a "cleaner" development environment .. I find the script-tag inline code approach to be a little buggy
    • You can provide better Intellectual Property protection, because your ASPX file won't expose all your source code
    • You can provide better code protection, because with inline script tags any idiot with NotePad can change the code!

    To use code-behind you place your code in a separate Class Library project.

    The Class Library should be strongly named and placed into the GAC after compilation.

     

    Then, when building your ASPX file, put a reference in the Page header that references your class name and assembly.

    You will need to use the fully qualified assembly reference

    (download and use the .Net Reflector to get this easily, any decent search engine should find a download link quickly enough)

     

    For example .. if you have an Assembly called "MyAssembly" with a public key token of "d3ee81fc11cb7d17"

    And say you have a class called "MyForm".

     

    When you build your custom Application Page (ASPX file) you add the following header:

     

    Code Block

     

    Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="MyAssemly.MyForm, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d3ee81fc11cb7d17"

     

    [EDIT - I've removed the < % and  % > tags because they were causing the text to fail]

     

     

     

    Note that the "Inherits" attribute will reference the fully-qualified classname and THEN the 4-way assembly reference.

     

    When you are building your Class Library you will need to add references to any of the server-side objects that you have referred to in your ASPX file.

     

    For Example:

     

    ASPX File

    Code Block

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <asp:Panel ID="pnlMain" runat="server"/>

    </< FONT>asp:Content>

     

     

    Class Library

    Code Block

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using Microsoft.SharePoint;

    using Microsoft.SharePoint.WebControls;

     

    namespace MyAssembly

    {

     

    public partial class MyForm : Microsoft.SharePoint.WebControls.LayoutsPageBase

    {

    protected Panel pnlMain = new Panel();

     

    protected void Page_Load(object sender, EventArgs e)

    {

    pnlMain.Controls.Add(new LiteralControl("Hello World"));

    }

    }

    }

     

     

    When it renders, your Application Page should now show the text "Hello World".

     

    If you load your Class Library, you should be able to Attach to the W3P.exe running on the SharePoint server and step through your code too!

     

    hope this helps

     

    MKeeper

    Tuesday, December 4, 2007 4:40 PM