Wrap a User Control inside a Web Part using WSPBuilder

来源:互联网 发布:翟欣欣事件始末 知乎 编辑:程序博客网 时间:2024/06/05 02:19

First: Create User Control

Second: Create a WSPBuild Project, Then create web part feature.

 

protected override void CreateChildControls()

        {

            if (!_error)

            {

                try

                {

 

                    base.CreateChildControls();

 

                    // Your code here...

                    //this.Controls.Add(new LiteralControl(this.MyProperty));

                    var uc = this.Page.LoadControl("/_controltemplates/ProductUT.ascx");

                    this.Controls.Add(uc);

                }

                catch (Exception ex)

                {

                    HandleException(ex);

                }

            }

        }

When I deployed this WSPProductUserCtrl.I run into the error following.

Error: Could not load the assembly. Make sure that it is compiled before accessing the page.

I searched through Internet. Then I found a solusion in under url.

(http://www.sharepointdev.net/sharepoint--development-programming/could-not-load-the-assembly-make-sure-that-it-is-compiled-before-accessing-the-page-error-while-45780.shtml).

Then I fixed this error.

 Solusion: I copied the webapplication.dll and another dll which is used in User Control solusion into _appbin(C:/Inetpub/wwwroot/wss/VirtualDirectories/41305/_app_bin) folder. Then the error disappeared.

 

 

 

Steps for embeded usercontrol in webpart

 

When you embed a user control in a web part you have to add the dll from the user control to the /bin directory of your site's virtual directory...

 

1) Create User Control
2) Publish User Control
3) Add User Control to web part
3a) You want to add the User Control that is found in /Projects/<User Control Project Name>/PrecompiledWeb/<User Control Project Name>
4) Add reference of User Control to Web Part project
4a) this is found in /Projects/<User Control Project Name>/PrecompiledWeb/<User Control Project Name>/bin/Debug
4b) the file will be a random generated name...you can reference the name from the User Control in the "control" tag at the top of the .ascx file
5) Build Web Part Project
6) Add User Control dll that you can find either in location from Step 4a or from the /bin/Debug folder of the Web Part project
6a) The dll goes in the Virtual Directory /bin folder (Example: /inetpub/wwwroot/wss/VirtualDirectories/80/bin)

 

 

 

 

If you run into the following error,you should try to make some changes

Error:Could not load type 'MainNavigationUserCtl.MainNavUserCtl'

Solusion:

<%

@ Control Language="C#" AutoEventWireup="true" Inherits="MainNavigationUserCtl.MainNavUserCtl,MainNavigationUserCtl,Version=1.0.0.0, Culture=neutral, PublicKeyToken=6d819def51007e47" %>

 

 

利用SharePoint:DelegateControl部署自定义UserControl
http://www.cnblogs.com/Lordan/archive/2008/05/16/1200625.html

 

原创粉丝点击