Add background code to SharePoint's internal pages
We know that SharePoint pages stored in a database are not directly added to the background code, which brings us a lot of inconvenience, such as the need to implement something on the page, you must use WebPart or custom control, even if it is simply a few lines of code behind the scenes. WSS 3.0 is based on ASP.net 2.0, and any technology used in the ASP.net site is also available at the WSS site. So we can also add background code to the pages of the WSS site.
The SharePoint pages stored in the database are divided into two departments, the master page and the content page, and we can add the background code for each of these two pages. Implementation is not the same, if the content page to add background code, We need to inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class, if you add back code to the motherboard page, we need to inherit from the System.Web.UI.MasterPage class, you should set the background code class and corresponding page Put it in the same name, but it's not necessary. As shown below:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Microsoft.SharePoint;
Using Microsoft.SharePoint.Publishing;
Using Microsoft.SharePoint.WebControls;
Namespace aa{
public class Aaclass:publishinglayoutpage {
}
}
This allows us to add the appropriate background code for the controls on the page. Let's say we have a button and a text box on our page, ID textbox1 and Button1, and add a Ckick event for the button, and when you click the button, write the current time to the text box, so you can write:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Microsoft.SharePoint;
Using Microsoft.SharePoint.Publishing;
Using Microsoft.SharePoint.WebControls;
Namespace AA
{
public class Aaclass:publishinglayoutpage
{
protected TextBox TextBox1;
protected Button button1;
protected override void OnInit (EventArgs e)
{
Base. OnInit (e);
Button1. Click + + new EventHandler (button1_click);
}
}
void Button1_Click (object sender, EventArgs e)
{
TextBox1. Text = DateTime.Now.ToString ();
}
}
On the Moss page, server controls are divided into ASP controls (namespace System.Web.UI.WebControls) and SharePoint controls (namespaces are Microsoft.SharePoint.WebControls). We can also declare SharePoint controls and add the appropriate actions to them.
After writing our background code, the code is generated into the corresponding bin directory (or GAC, remember strong naming), add a row in the Web.config file, <safecontrol assembly= "namespace=" "typename=" * " Safe= "True"/> where assembly and namespace can be obtained by reflector, and then we also need to rewrite the page on the page:
<%@ Page meta:progid= "SharePoint.WebPartPages.Document" language= "C #" inherits= "Mosscodebehind.codebehind, Mosscodebehind, version=1.0.0.0, Culture=neutral, Publickeytoken=null "%>
If it is a master page, this adds
<%@ Master language= "C #" inherits= "Mosscodebehind.codebehind,mosscodebehind, version=1.0.0.0, Culture=neutral, Publickeytoken=null "%>
However, after the rewrite, you cannot open the page in the design window.
In this way, developers can do development like asp.net, for example, we can rewrite the OnLoad event to implement binding data to the page's controls.
Interested friends can try to meet our many needs.