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

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

March 2008 - Posts

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

No SharePoint Site exists at the specified URL

 

My present project after DotNetNuke was Microsoft Office Sharepoint Server 2007. It was another fun and challenging experience on my part. I have lots of research and lots of problem to solve. One thing that was a little bit hard on this project was creating web parts. Web parts is a counterpart of modules in DNN. But here, you don't design your Web Parts on a designer just what am I doing in modules. Instead, you either create it in Sharepoint Designer or use template and create one from there.

In using template, after creating your code, build your project of course, and according to Microsoft's link: http://msdn2.microsoft.com/en-us/library/ms948909.aspx and some other links, deploying was as easy as pressing F5. But I always got this error message: "No SharePoint Site exists at the specified URL". It was kind confusing because I did every configuration I need to set but it was all nothing. Then what I did is instead of putting the actual URL of my sharepoint site to the Build -> Output Path, I instead define my URL at Debug -> Start Action -> Start Browser with URL . And rebuild then deployed my project. Now everything works fine. Smile

 

Certificate of Excellence as Microsoft Certified Technology Specialist .NET 2.0: Web Applications

 

It was a normal day yesterday and as I entered my house, I saw this large envelope and and I knew that was from Microsoft. At last, I received my Certificate of Excellence as Microsoft Certified Technology Specialist .NET 2.0 Web Applications. After more than 2 years of hardwork and patience, at last, it was paid off. It took me almost 2 years before I took the second core exam which is 070-528. It was hard and it needs me more time to study while working. But it was a little bit easy because the experiece I've got from my current company (Gurango Software Corporation). I never knew that I will be a Web Developer there because when I was still intern, we task to do something which will determine if what department we wil be deployed. But thank GOD, I was deployed on the Web Development area.

Well, receiving this is a big hope for another certification. A hope to increase more my knowledge about Microsoft technologies. I hope I received my wallet card as soon as possible. I want to know what it looks like.

 

// Willy David Jr

MCP ID: 3804375