Summary
There are now two different Web parts. The old WSS-style webpart depends on Microsoft. Sharepoint. dll and must inherit from the webpart base class defined in WSS 2.0. Its namespace is Microsoft. Sharepoint. webpartpages. The new ASP-style webpart depends on system. web. DLL, which must be inherited from a different ASP.. NET 2.0 defines the webpart base class, and its namespace is system. web. UI. webcontrols. webparts.
We will start with a simple hello word Web part:
Code Design:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.UI.WebControls.WebParts;using System.Xml.Serialization;using System.Web.UI;namespace LearnWriteWebPart.Webpart{ [ToolboxData("<{0}:SampleWebPart runat=server></{0}:SampleWebPart>")] [XmlRoot(Namespace = "LearnWriteWebPart.Webpart")] public class SampleWebPart : WebPart { private string _Text = "Hello World!"; [WebBrowsable(true), Personalizable(true)] public string Text { get { return _Text; } set { _Text = value; } } protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.Write(_Text); } }}
In the spring blog we have created, perform the following operations to add the webpart to the site:
1. Activate the self-written webpart. Click populate gallery as follows.
2. Load the Web Part in the spring blog. First click Edit page.
3. Click Add web a part and select our samplewebpart.
4. The results are the same as we thought.
Next, let's make a complicated part. to log on to the web part, add a user control and name it loginusercontrol. ascx,
The Code is designed as follows:
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "loginusercontrol. ascx. CS "inherits =" webusercontrol_loginusercontrol "%> <style type =" text/CSS ">. style1 {width: 32%; Height: 28px ;}. style2 {width: 128px ;}</style> <Table class = "style1"> <tr> <TD class = "style2"> <asp: label id = "lbluseraccount" runat = "server" text = "useraccount:"> </ASP: Label> </TD> <asp: textbox id = "txtuseraccount" runat = "server" tabindex = "1"> </ASP: textbox> <asp: requiredfieldvalidator id = "rfvuseraccount" runat = "server" controltovalidate = "txtuseraccount" errormessage = "username cannot be blank"> </ASP: requiredfieldvalidator> </TD> </tr> <TD class = "style2"> <asp: Label id = "lblpassword" runat = "server" text = "password: "> </ASP: Label> </TD> <asp: textbox id = "txtpassword" runat = "server" textmode = "password" tabindex = "2"> </ASP: textbox> <asp: requiredfieldvalidator id = "rfvpassword" runat = "server" controltovalidate = "txtpassword" errormessage = "password cannot be blank"> </ASP: requiredfieldvalidator> </TD> </tr> <TD class = "style2"> & nbsp; </TD> <asp: button id = "btnlogin" runat = "server" tabindex = "3" onclick = "btnlogin_click" text = "login"/> <asp: label id = "lblresult" runat = "server" bordercolor = "red" forecolor = "red"> </ASP: Label> </TD> </tr> </table>
The background code is as follows:
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using LearnWriteWebPart.BE;using LearnWriteWebPart.BO;using LearnWriteWebPart.Util;public partial class WebUserControl_LoginUserControl : System.Web.UI.UserControl{ /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SetInitial(); } } protected void btnLogin_Click(object sender, EventArgs e) { if (!CheckUserAccountLength()) return; if (!CheckPasswordLength()) return; this.CheckLogin(); } #region Private Method /// <summary> /// Initial Control /// </summary> private void SetInitial() { txtUserAccount.Focus(); CodeHelper.DisableIMEModes(new TextBox[] { txtUserAccount, txtPassword}); } /// <summary> /// Check login /// </summary> private void CheckLogin() { UserBE userBE = new UserBE(); UserBO userBO = new UserBO(); userBE.UserAccount = txtUserAccount.Text.Trim(); userBE.Password = txtPassword.Text.Trim(); if (userBO.CheckUserLogin(userBE)) { lblResult.Text = Constants.SUSSCESSFULLOGIN_USER; } else { lblResult.Text = Constants.ERRORLOGIN_USER; } } /// <summary> /// check password length /// </summary> /// <returns></returns> private bool CheckPasswordLength() { int length = txtPassword.Text.Trim().Length; if (length < 6) { lblResult.Text = Constants.PASSWORDMINLENGTH_USER; return false; } else if (length > 20) { lblResult.Text = Constants.PASSWORDMAXLENGTH_USER; return false; } return true; } /// <summary> /// Check user account length /// </summary> /// <returns></returns> private bool CheckUserAccountLength() { int length = txtUserAccount.Text.Trim().Length; if (length > 20) { lblResult.Text = Constants.USERACCOUNTMAXLENGTH_USER; return false; } return true; } #endregion}
I believe everyone knows the code of userbe and userbo, so I will not post it.
The webpart code is as follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.UI.WebControls.WebParts;using System.Xml.Serialization;using System.Web.UI;using LearnWriteWebPart.Util;namespace LearnWriteWebPart.Webpart{ [ToolboxData("<{0}:UserWebPart runat=server></{0}:UserWebPart>")] [XmlRoot(Namespace = "LearnWriteWebPart.Webpart")] public class UserWebPart : WebPart { #region [Private Variable] private string _ListName = string.Empty; private string _Url = string.Empty; private string TheListName = Constants.LISTNAME_USER; private UserControl _userControl; #endregion #region [Custom Properties] /// <summary> /// This list naem /// </summary> [WebBrowsable(false), Personalizable(true)] public string ListName { get { return _ListName; } set { _ListName = value; } } /// <summary> /// The list Url /// </summary> [WebBrowsable(false), Personalizable(true)] public string Url { get { return _Url; } set { _Url = value; } } #endregion #region [Constructors] /// <summary> /// The sample constructor /// </summary> public UserWebPart() { this.ListName = TheListName; } #endregion #region [Override Methods] /// <summary> /// Override method to OnInit method /// </summary> /// <param name="e">The EventsArgs object</param> protected override void OnInit(EventArgs e) { base.OnInit(e); SetWebPartTitleAndUrlWhenAdded(this.ListName, this.Url); AddControlToWebPart(); } #endregion #region [Private Methods] /// <summary> /// Add Login Control /// </summary> private void AddControlToWebPart() { Type controlType = Type.GetType("ASP.webusercontrol_loginusercontrol_ascx,Web_deploy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567f94a516f5"); _userControl = (UserControl)this.Page.LoadControl(controlType, null); this.Controls.Add(_userControl); } /// <summary> /// Set webpart title and url /// </summary> /// <param name="ListName"></param> /// <param name="Url"></param> private void SetWebPartTitleAndUrlWhenAdded(string ListName, string Url) { this.Title = this.ListName; this.TitleUrl = this.Url; } #endregion }}
The result is as follows:
See the picture we expected.
Next, we will create a webpart and editwebpart of the user's Xiangxi information.
Author: Spring Yang
Source: http://www.cnblogs.com/springyangwc/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.