Solved

SharePoint Web Part Help

Posted on 2011-02-21
3
572 Views
Last Modified: 2012-06-27
Please see below code below.

I am trying to create a webpart that allows a person to create sub-sites using a existing site template that's provided in a drop down list.

I get the following error when I run the code:

Error      2      The event 'System.Web.UI.WebControls.Button.Click' can only appear on the left hand side of += or -=      line 90

Error      3      The best overloaded method match for 'Microsoft.SharePoint.SPWebCollection.Add(string, string, string, uint, string, bool, bool)' has some invalid arguments         line 111

Error      4      Argument '1': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'    line 111

Error      5      Argument '2': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'    line 111

Error      6      Argument '3': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'   line 111
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace CreateSolutionSiteWebPart
{
    [Guid("0b2ced02-2687-4c50-9b57-f9b2ea5f31cc")]
    public class CreateSolutionSiteWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        // declare all your controls here so they don't pass out of scope
        Label lbl_siteName = null;
        Label lbl_siteDesc = null;
        Label lbl_siteUrl = null;
        Label lbl_drpDwnList = null;
        TextBox txt_siteName = null;
        TextBox txt_siteDesc = null;
        TextBox txt_siteUrl = null;
        DropDownList drp_siteTempLates = null;
        Button btn_Submit = null;
        Label lbl_uPermissions = null;
        CheckBox txt_uPermissions = null;

        public CreateSolutionSiteWebPart()
        {
        }

        // configure and populate all your controls here
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
            lbl_siteName = new Label();
            lbl_siteDesc = new Label();
            lbl_siteUrl = new Label();
            lbl_drpDwnList = new Label();

            txt_siteName = new TextBox();
            txt_siteDesc = new TextBox();
            txt_siteUrl = new TextBox();
            drp_siteTempLates = new DropDownList();

            lbl_siteName.Text = "Name: ";
            Controls.Add(lbl_siteName);

            txt_siteName.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteName);

            lbl_siteDesc.Text = "Description: ";
            Controls.Add(lbl_siteDesc);

            txt_siteDesc.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteDesc);

            lbl_siteUrl.Text = "Url: ";
            Controls.Add(lbl_siteUrl);

            txt_siteUrl.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteUrl);

            lbl_drpDwnList.Text = "Make a Selection";
            Controls.Add(lbl_drpDwnList);

            SPSite site;
            SPWebTemplateCollection coll;
            site = SPContext.Current.Site;
            coll = site.GetWebTemplates(SPContext.Current.RegionalSettings.LocaleId);
            for (int i = 0; i < coll.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = coll[i].Title;
                li.Value = coll[i].Name;
                drp_siteTempLates.Items.Add(li);
            }


            btn_Submit = new Button();
            btn_Submit.Width = new Unit(50, UnitType.Pixel);
            btn_Submit.Text = "Submit";
            btn_Submit.Click += new EventHandler(btn_Submit.Click);  <- Line 90
            Controls.Add(btn_Submit);

        }

        void OnLoad(object sender, EventArgs e)
        {
            EnsureChildControls();
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            Guid siteID = SPContext.Current.Site.ID;
            Guid webSiteID = SPContext.Current.Web.ID;

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(siteID))
                {
                    SPWeb myWeb = site.OpenWeb();
                    SPWebCollection mySite = myWeb.Webs;
                    SPWeb newSite = mySite.Add(txt_siteUrl, txt_siteName, txt_siteDesc, 1033, "STS#1", true, false);  <-- Line 111
                }

            });
        }
        // remember to have functions to populate the dropdown
        // and create the site

        // you can use this function to get the values of controls 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

        }

        // use this function to layout your controls
        protected void Render(HtmlTextWriter writer)
        {
            // lbltest.RenderControl(writer);   put down the control
            // writer.Write(@""&nbsp;");        add a space at the end
            writer.Write(@"<Table width='35%' align='center'><tr><td>");
            lbl_siteName.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteName.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td>");
            lbl_siteDesc.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteDesc.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td>");
            lbl_siteUrl.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteUrl.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td colspan='2'>");
            btn_Submit.RenderControl(writer);
            writer.Write(@"</td></tr></table>");
        }
    }
}

Open in new window

0
Comment
Question by:Isaac
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
Line 90 should say:
btn_Submit.Click += new EventHandler(btn_Submit_Click);

Open in new window

Line 111 should probably be:
SPWeb newSite = mySite.Add(txt_siteUrl.Text, txt_siteName.Text, txt_siteDesc.Text, 1033, "STS#1", true, false);

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
Thanks!
0
 
LVL 5

Author Closing Comment

by:Isaac
Comment Utility
Excellent!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now