SharePoint (WSS) Learning (6) Use ascx in webpart

Source: Internet
Author: User
It is very troublesome to design the UI by adding controls to the webpart. However, some simple functions do not require the page creation method. It is much easier to use ascx in the webpart. I think of N (> 2) methods to implement it, and introduce a more "elegant" implementation method.

This "Elegance" refers:

· You are not allowed to manually deploy multiple copies of files.
· The development method of ascx is basically the same as that of ASP. NET.
. Add a webpart to the page.

Create a webpart and add a new project module. For details, refer:

SharePoint (WSS) Learning (2) Developing webpart
SharePoint (WSS) Learning (4) add ASPX page

Create an Asp.net web application, design and encode the user control ascx, and add *. ascx, *. ascx. CS, *. ascx. Designer. CS to the module item.
The project view is as follows:

Configure the URL of the ascx. Refer to the configuration method of the newly created ASPX page.
The module. XML content is:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Elements ID = "E5e0924d-b286-4d9c-b270-063f446df208" Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Module Name = "Ascx" URL = "Controls" >
< File Path = "Sampleascx. ascx" URL = "Sampleascx. ascx" />
</ Module >
</ Elements >

In the webpart *. to load ascx In the CS file, the Code is as follows: using system;
using system. runtime. interopservices;
using system. web. ui;
using system. web. UI. webcontrols;

NamespaceXianfen. net. samplewebpartwithascx
{
[GUID ("A4175a9d-9b78-4eb4-8159-11bc6c5a6cfa")]
Public ClassMysamplewebpartwithascx: system. Web. UI. webcontrols. webparts. webpart
{
PublicMysamplewebpartwithascx ()
{
}

Protected Override Void Createchildcontrols ()
{
Try
{
// Load ascx
Control C = This . Page. loadcontrol ( " ~ /Controls/sampleascx. ascx " );
This . Controls. Add (C );
}
Catch (Exception ex)
{
// An error is displayed. This code is removed from the official release.
Label LBL = New Label ();
LBL. Text = Ex. message;
This . Controls. Add (LBL );
}
}
}
}

F5 compilation and deployment, follow the previous stepsArticleDeploy and activate the webpart and module in the described method, and add the webpart to the page. The execution result is as follows:

The error message "xianfen. net. webpartwithascx. sampleascx" is not found.ProgramSet. That is, the background code of the dynamic page cannot be found in the previous article. Open other deployed ascx or Aspx. You can see:
Inherits ="Microsoft. Sharepoint. webpartpages. webpartpage, Microsoft. Sharepoint, version = 12.0.0.0,Culture = neutral, publickeytoken = 71e9bce111e9429c"And so on. It contains the full type name and the full Assembly name. Change the ascx header slightly,
The content of ascx is:

<% @ Control Language = " C # " Autoeventwireup = " True " Inherits = " Xianfen. net. webpartwithascx. sampleascx, samplewebpartwithascx, version = 1.0.0.0, culture = neutral, publickeytoken = 9f4da00417c38ec5 " %>
< Div Style = "Width: 220px; padding: 10px; Border: solid 1px # CCC; Background-color: # ECC ;" >
< ASP: textbox ID = "Txt1" Runat = "Server" > </ ASP: textbox >
< ASP: button ID = "Btn1" Runat = "Server" Text = "Click" Onclick = "Btn1_click" />
</ Div >

Check whether the full name of the Assembly can be reflector ,:

After modification, deployment, and activation, the webpart can run correctly,

Floating remote blog: http://www.cnblogs.com/zxjay/
Author: piaoyao (Zhou Zhenxing)

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.