Programming is Passion,Software Development is an Adventure- Willy David Jr

Programming is Passion,Software Development is an Adventure- Willy David Jr

Creating Complex Web Parts in Sharepoint 2007 using Web User Control

 

My task this few days was to research on how to create comple Web Parts in Sharepoint 2007. Ideally, I create my web parts using code-behind code. In other words, I create the controls dynamically. This was gruesome in my part, because from positioning to event-handlers may take a lot of time, debugging, and most specially in designing. Take a look at my sample code I used to design a very simple web part:

 

protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.

            Button myButton = new Button();
            myButton.Text = "Click";
            myButton.Width = 50;
            myButton.Height = 50;

            myButton.Click += new EventHandler(myButton_Click);

            this.Controls.Add(myButton);

        }

        void myButton_Click(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            myLabelPublic = new Label();
            myLabelPublic.Text = "For testing only. For testing only. For testing only.";
            this.Controls.Add(myLabelPublic);

        }

Basically, this code generate a button on it's page load. After the load, if user clicks the button, obviously, a new Label will generate displaying the message. This is not the way I want to develop my Web Parts, because it takes a lot of time.

So after a long time research, I found another way to create not just simple but you can make it complex using Web User Control. I did suspect Web User Control at the first place but I don't know how to figure it out. Anyway, here are the steps:

1.) First, of course, create your Web User Control. You have the option on how you create it, but in my part, I create it using ASP.NET 2.0 IDE. What I usually did is I create a blank page, then I add a new item which is Web User Control. Take note to uncheck the option which separate your code in a separate file.

2.) Second, design your Web User Control (ascx), from its physical design to its logic coding.

3.) Copy your ascx file to this directory: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES

4.) Then use this sample code as your reference for adding your Web User Control file in your .cs file:

 

  protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
     
             this.Controls.Clear();
             userControl = (UserControl)this.Page.LoadControl(@"/_controltemplates/Test.ascx");
             this.Controls.Add(userControl);
         }


        protected override void RenderContents(HtmlTextWriter writer)
        {
            //base.RenderContents(writer);
            userControl.RenderControl(writer);
        }

5.) Last build then deploy your site. Make sure your output is the URL of your sharepoint site for successful deployment of your Web Part. Smile

 

// Willy David Jr

Comments

Reetha said:

Hi Willy David Jr,

Thanks a lot for providing the useful information.

It is very much useful for me.

# April 15, 2008 7:58 AM

willydavidjr said:

Your welcome Reetha!

# April 15, 2008 6:58 PM

Tony said:

I am getting the error:

"The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file."

on the line below

"userControl = (UserControl)this.Page.LoadControl(@"/_controltemplates/Test.ascx");"

Did you have to change anything in the web.config of the site collection?

# April 16, 2008 7:34 PM

Tony said:

I got it to work by changing the site collection's web.config trust level from "WSS_Mininal" to "Full"

# April 16, 2008 7:48 PM

willydavidjr said:

Thanks for that Tony, I will take note of it. Thanks again!

# April 16, 2008 10:38 PM

young said:

This is great!

# May 6, 2008 1:31 PM

Saurabh said:

It doesn't work for me......

'C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\UC.ascx' is not a valid virtual path.   at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)

this error is cuming whenever i am adding the webpart containing UserControl

# May 9, 2008 3:36 AM

willydavidjr said:

Hi Saurabh, are you sure you have this directory: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\ ?

# May 18, 2008 8:17 AM

Ahmed said:

this error occured:

"The file '/UC/MyUCWebPart.ascx' does not exist."

could u plz guide me, how to solve this??

NOTE: i don't want to modify my security level as i use a custom security policy.

Thanks for greate post.

# May 20, 2008 12:05 AM

willydavidjr said:

Hi Ahmed, are you did the step in number 3?

3.) Copy your ascx file to this directory: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES

There is no modification in custom security policy here. All you need is proper deployment of ascx file and some lines of code just what I have posted above. Try to create this on a fresh web part, and some controls on an ascx file.

# May 23, 2008 12:13 AM

KissMyAss said:

Great job boy !!

# May 26, 2008 1:18 AM

Tran Trung Duc said:

Thank you verry much, it's great.

# June 4, 2008 8:58 PM