简体   繁体   中英

Using asp.net user control in sharepoint custom webpart

So I need to create a webpart. I'd like some control over the design process, and creating the code for the controls then build/compile wait for app-recycle and then F5 the sharepoint, rinse and repeat site is a pain.

I was reading that it's possible to just create a regular user control, and have that load in the webpart. Ideal I think - but!

What about all of the sharepoint stuff? Like the properties, and interacting with sharepoint lists and so on? How would I accomplish this best from the user control?

What I need exactly, is a webpart displaying firstly a set of information of one item in a sharepoint list, and secondly it needs to show a scrollable list of items from another sharepoint list.

Is there a fancy way of getting pretty much the identical 'environment' or should I just go like:

using(SPSite site = new SPSite("http://mysite/")){
    using(SPWeb web = new SPWeb("myWeb")){
         //Fill a listbox with web.lists["myList"];
    }
}

From the code-behind of my usercontrol?

Yes, that is the write way.

It's just that instead of writing webpart directly, you can write the usercontrol and then host it in a wrapper webpart.

The advantage of this approach is that you can have designer for creating UI elements which is available while creating usercontrol. For sharepoint 2007, the wrapper webpart is already available called smart part. To smartpart, you can just give the location of your user control(.ascx) to load.

In sharepoint 2010, the smartpart can be built out of the box using Visual Studio 2010.

http://smartpart.codeplex.com/

You have to create simple web part which extends WebPart class. Inside it you may add your own control by overriding CreateChildControls method. You may pass any required arguments in the constructor of your control. As for the properties you may describe them in wrapper web part and map it to control properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM