Solved

SharePoint Web Part Help

Posted on 2011-02-21
3
587 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
ID: 34943975
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
ID: 34944053
Thanks!
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 34944056
Excellent!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

11 Experts available now in Live!

Get 1:1 Help Now