Monday, April 21, 2008

Code Behind in Sharepoint Designer

Create a new class Library
namespace CodeBehindTest
{
public class Sample : Page
{
protected Label lblText;//the same name as in SPD
protected Button btn;//the same name as in SPD

protected override void OnInit(EventArgs e)
{
btn.Click += new EventHandler(btn_Click);//Attach the event to the button
}
protected void btn_Click(object sender, EventArgs e)

{
lblText.Text = "Testing successfully";
}

}
}
Sign you assembly using the signing tool in VS
Build your solution, and drag drop the dll to GAC (c:\windows\assembly)
restart your IIS iisreset /noforce
Register you assembly as safe control in the web.config of the site
<SafeControl Assembly="CodeBehindTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eef1ac0da5ab2a1f" Namespace="CodeBehindTest" TypeName="*" Safe="True" />
Create your ASPX Page in Sharepoint Designer
<%@ Page Language="C#" MasterPageFile="~masterurl/default.master"Inherits="CodeBehindTest.Sample,CodeBehindTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eef1ac0da5ab2a1f" %>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server"> <asp:Label ID="lblText" runat="server"></asp:Label> <asp:Button runat="server" Text="Click Me" ID="btn"></asp:Button> </asp:Content>

Regards



7 comments:

Anonymous said...

thank you, thank you, thank you!! now I can expand this and display a sharepoint library on click of the button!! thanks

Anonymous said...

Thanks, being trying to work this out for a long time!

Anonymous said...

Using this method, I am trying to use a page_load routine (simillar to vanilla asp.net) but it never executes. I looked everywhere and everyone who talks about the code-behind uses there overriden OnInit and a button click event.

How can this be done?

Bilal Shouman said...

What do u wanna use in page_load event??

Regards

Anonymous said...

Bilal,

I wish to do a few things when the page loads, one of which is to use WSS API to get list items and bind to a DataGrid.

Anonymous said...

Bilal,

I used the OnLoad event and put my code in there and that did the trick for me. Thanks for you help by the way. Kepp up the good work.

Bilal Shouman said...

Im glad ur problem is solved my friend :)