Add background code to SharePoint's internal pages

Source: Internet
Author: User
Tags current time
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"/&gt 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.

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.