Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Regarding CodeBehind in SharePoint

  Asked By: Carlton    Date: May 01    Category: Sharepoint    Views: 4425

I am seeing many articles on sharepoint forums regarding accessing the
code-behind pages
I treid several and end up with no sucess!! Is it really possible to access
the code-behind in sharepoin designed pages.(a site page applied with master
page )

Pls can anybody sugges me perfect steps or a article to achive this
My requirement is to create some custom aspx page(which will nd shud access
them on sharepoint site with same look and feel(applied with master page)!!
{I tried adding class library dll(which is extended with system.web.UI
Controls) in sites bin directory and refering those as safecontrols in
web.config and used the same server controls in sharepoint aspx page to refer
the server controls }But in the end i am unble to access the methods on class
library through the asp button on sharepoint aspx page!!
(It says the onclick event cann't be used (but it shows onclick event in the
list) by the way what is difference b/w OnClick and OnClientClick)

Hope you got wht my problem is!



4 Answers Found

Answer #1    Answered By: Joe Hart     Answered On: May 01

As I understand this problem is that you may have done the code-behind correct,
I don't understand fully how you done it. But first its worth to try this first:

I understand you that you get an exception saying onclick event not allowed.
I've got this exception to. You need another directive in web.config:


As I remember there is not much information about it, neither on "google" or in
the sdk. But there is little In sdk, search for PageParserPath.

In my case I had set up a page with code behind and got this exception. In my
case I wanted to use the customized page under a subsite called "ProjectPortal",
that's why I set VirtualPath="/ProjectPortal/*". If you want your code to work
on the whole sitecollection, use "/*". Its not much help about the attributes on
the PageParserPath tag. Then I added the following in web.config:



<SafeMode ...>


<PageParserPath VirtualPath="/ProjectPortal/*"
CompilationMode="Auto" AllowServerSideScript="true" IncludeSubFolders="true" />







Answer #2    Answered By: Jorge Cole     Answered On: May 01

Sorry, maybe I'm wrong. I think this tag is needed if you want to use inline
scripts in your aspx, <%# MethodCallHere() %>.

I remembered wrong.

Answer #3    Answered By: Rodolfo Tate     Answered On: May 01

As I gave you a wrong answer before I feel I have to try to help you!

I have made a simple test:

1. In Visual Studio 2005 I create a Web Application Project in C#. Need to have
the Web Application Project Template installed. This is included in Visual
Studio 2005 SP1.

2. Add a button to the Default.aspx and give it a button click event. In my
click event I just add 'Response.Write("<h1>Button clicked</h1>");'

3. Build your web app.

4. Run and test so it works.

5.Rename a Default.aspx in some SiteTemplate to Default.aspx.old and copy your
Default.aspx to this SiteTemplate folder. For example: "C:\Program Files\Common
Files\Microsoft Shared\web server

6. Put your dll in the bin folder:
"C:\Inetpub\wwwroot\wss\VirtualDirectories\<YourSharePointAppFolder - often

7. In C:\Inetpub\wwwroot\wss\VirtualDirectories\<YourSharePointAppFolder - often
80>\web.config add it as safecontrol.


<SafeControl Assembly="SharePoint.Test" Namespace="SharePoint.Test"
TypeName="*" Safe="True" AllowRemoteDesigner="True" />


8. IISRESET (to be certain)

9. Create a "Collaboration - Blank Site" if you don't already have another sts

10. Test your site, default.aspx and click the button. It should work

My aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="SharePoint.Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>



<form id="form1" runat="server">


<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />





My aspx.cs code:

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;

namespace SharePoint.Test


public partial class _Default : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)



protected void Button1_Click(object sender, EventArgs e)


Response.Write("<h1>Button clicked</h1>");




My aspx.designer.cs code:

namespace SharePoint.Test {

public partial class _Default {

protected System.Web.UI.HtmlControls.HtmlForm form1;

protected System.Web.UI.WebControls.Button Button1;



Ok I know this is very simple but I think its good to know what is needed in
web.config and so on to get it working.

Next steps:

Add a master page definition to your default.aspx

Inherit from Microsoft.SharePoint.WebPartPages.WebPartPage instead of
System.Web.UI.Page if you want to use webpartpages in your page.

Add a reference to Microsoft.SharePoint dll to start coding against the object

Copy code from some existing default.aspx (sts) to get webpartzones and other
references to webcontrols etc.

Maybe you think some of this is basic for you. This is just the way I want to
explain things.

Answer #4    Answered By: Jesus Foster     Answered On: May 01

I would like to explain what exactly I have done
Just to make you clear of what I am trying I am giving the detailed steps

Step 1 : Created the following calss library codeBehind

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NewCodeBehind
public class CodeAccess: Page
protected TextBox TextInput;
protected Button Trigger;
protected Literal TextOutPut;
protected Literal TimeStamp;
protected void Trigger_Onclick(object Sender, EventArgs e)
TextOutPut.Text = TextInput.Text.Trim();
TimeStamp.Text = DateTime.Now.ToString();
Step 2:Build the class library and copied the NewCodeBehind.dll to my sites's
bin directory

Changes to Web.config
<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10"
TotalFileDependencies="50" AllowPageLevelTrace="false">
<PageParserPath VirtualPath="/*" CompilationMode="Always"
AllowServerSideScript="true" IncludeSubFolders="true" />
<SafeControl Assembly="NewCodeBehind" Namespace="NewCodeBehind" TypeName="*"
Safe="True" />

Step 4:Created A new codeBehind.aspx in sharepoint  designer with this code
<%@ Page Language="C#" masterpagefile="~masterurl/default.master"
title="Server Controls" Debug="true"
meta:progid="SharePoint.WebPartPage.Document" %>
<asp:content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">First
CodeBehind Page On SharePoint</asp:content>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<h2>Check Out the CodeBehind Functionality</h2>
<h3>Enter Some Text here and Press the Button</h3>
<asp:TextBox Id="TextInput" runat="server"/> 
<asp:Button ID="Trigger" runat="server" Text="Submit"
<p>The text Enterd was <b>
<asp:Literal Id="TextOutPut" runat="server"/></b>
And was submitted at <b><asp:Literal Id="TimeStamp" runat="server"/></b>.</p>

hence the design looks like CodeBehind.doc (check the attached file)

Intially I was able to see the codebehind screen on sharepoint site
but after changing the OnClick=<%#NewCodeBehind.CodeAccess.Trigger_Onclick%>
Unable to load the codebehind page ....but end  up with
these couple of erros and fixed most of those like

1)An error occurred during the compilation of the requested file, or one of
its dependencies. Cannot access protected member
'NewCodeBehind.CodeAccess.Trigger_Onclick(object, System.EventArgs)' via a
qualifier of type 'NewCodeBehind.CodeAccess'; the qualifier must be of type
'ASP.fe274d22_3dd8_4769_9cd1_2614925cabaf_1727459910' (or derived from it)

2)An error occurred during the compilation of the requested file, or one of
its dependencies. The best overloaded method match for
'System.Convert.ToString(object, System.IFormatProvider)' has some invalid
Troubleshoot issues with Windows SharePoint Services.

Now I commented the DateTime.Now.ToString();
{TimeStamp.Text = "Hello World";//DateTime.Now.ToString();}
but still at the same error!!?
I struck up with this second error and unable to check which way to goahead!!

Any Idea Sir!?

Didn't find what you were looking for? Find more on Regarding CodeBehind in SharePoint Or get search suggestion and latest updates.