[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SharePoint Web Part Help

Posted on 2011-02-21
3
Medium Priority
?
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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