Skip to main content

UserControl in WebPart

some of you maybe get used of using Web Part extension in Visual Studio Project and build the UI using code without the UI there but i found a simple alternative using User Control and in the Web Part is just some code to load that UserControl. U can debug ur code and it's with UI :D

here is what i'm doing..

User Control Project:
1. Create New ASP.NET Web Application.
2. Add New User Control into the Project.
3. Design your User Control, u can add code behind or anything like that.
4. Test the User Control by dragging it to Default.aspx page that automatically created from step 1.
5. Build your Application for the last time (it will have .dll inside bin folder of your application).

for now we leave the User Control Project, we go on making our Web Part Object
1. Create New Web Part Project,
2. You can delete the default Webpart project that created then create new one to suit the name or u can use the existing one.
3. Add reference to Microsoft.Sharepoint.dll
4. Add some directive
5. Add this variable UserControl userControl; (u can use anything u want)
6. Add this line of code to your CreateChildControls method
userControl = (UserControl)Page.LoadControl(@"/_controltemplates/your_user_control_file_here.ascx");
this.Controls.Add(userControl);

7. Add this line to your Render method
userControl.RenderControl(writer);

8. Build the Web Part Project.
here is the tricky part:
1. Copy the .dll from both project (Web Part and User Control) to your virtual directory bin folder.
2. Edit web.config so it's Full trust
3. Copy the .ascx to ControlTemplate directory that is under C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES in my Computer
4. do IIS reset
5. Add the WebPart in your MOSS / WSS Site and add them to the page

u can see the User Control now right?

in case any changes u made with User Control, just make sure to copy back the newly .dll to the bin folder and the .ascx to CONTROLTEMPLATE directory

PS: no need to GAC it..for the security, i think it's gonna be a problem if u want tight security because all the .dll is assume to be safe??? never explore it, if anyone have a comment about this..please share it

that's all
see u

Comments

Popular posts from this blog

SBO - Internal Error (-5002) Occured in Inventory Transfer

this is something that not well documented in SBO, it took me several hours to look at Google and can't find anything about it. Thank God friend of mine got answer for this one.So the Sympton is something like this, u need to make Inventory Transfer for Batch Item and Serial Item in one single Transaction so the Transaction is the combination of it. ex in detail lines: 1. Serial Item 2. Batch Item 3. Batch Item 4. Batch Item 5. Serial Item 6. Serial Item . When u reach this code oStockTransfer.Add it will return an error "Internal Error (-5002) Occured". This is because ur details is not in order, meaning u need to order the line with combination of Batch first then Serial or Serial first then Batch so be careful with this thing :D see u

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then

Resolve Collation error in SQL Server

i often get this error when trying to JOIN some table Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation. u need to use COLLATION when performing the JOIN, something like this SELECT field1 FROM table1 INNER JOIN table2 ON table1.field3 COLLATE DATABASE_DEFAULT = table2.field4 COLLATE DATABASE_DEFAULT i found this when trying to JOIN, maybe it affect other areas..until i found one see u