Add background code to a SharePoint page

Source: Internet
Author: User

Add background code to a SharePoint page

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 a WebPart or custom control, even if it is simply a few lines of code behind. While WSS 3.0 is based on ASP. NET 2.0, any technology used in the ASP. NET site is also available in the WSS site. So we can also add background code to the WSS site's pages.

The SharePoint pages that are stored in the database are divided into two departments, a master page and a content page, where we can add code behind each of these pages. Implementation in a different way, if you add background code for the content page, We need to inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class, if we add the background code for the master page, we need to inherit from the System.Web.UI.MasterPage class, you should set the background code class and the corresponding page The same name, but it is not required. 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 that we have a button and a text box on our page with IDs TextBox1 and Button1, and a Ckick event for the button, and when you click on it, the current time is written to the text box, so to 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, the 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 appropriate actions for them.
After writing our backend code, generate the code into the corresponding bin directory (or GAC, remember Strong name), add a line in the Web. config file, <safecontrol assembly= "" namespace= "" typename= "*" Safe= "True"/&gt, where assembly and namespace can be obtained through reflector, 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 this is a motherboard page, add
<%@ Master language= "C #" inherits= "Mosscodebehind.codebehind,mosscodebehind, version=1.0.0.0, Culture=neutral, Publickeytoken=null "%>

After rewriting it, however, you cannot open the page in the design window.

In this way, developers can do the same as in ASP., for example, we can rewrite the OnLoad event to bind the data to the page's controls.
Interested friends can try, can meet our many needs.

Add background code to a SharePoint page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.