Search This Blog

Thursday, March 12, 2015

Submit Operation From Web Part Dynamic Creation of Control





using System;
using System.ComponentModel;
using System.Web;
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.Utilities;


namespace EUDGridView.CustomEmployeeSubmittWebPart
{
    [ToolboxItemAttribute(false)]
    public class CustomEmployeeSubmittWebPart : WebPart
    {
        TextBox oTextTitle;
        TextBox oTextName;
        TextBox oTextDesignation;
        Label oLabelMessage;
        Button oButtonSubmit;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            oTextTitle = new TextBox();
            this.Controls.Add(oTextTitle);

            oTextName = new TextBox();
            this.Controls.Add(oTextName);

            oTextDesignation = new TextBox();
            this.Controls.Add(oTextDesignation);

            oLabelMessage = new Label();
            this.Controls.Add(oLabelMessage);

            oButtonSubmit = new Button();
            oButtonSubmit.Text = "Submit";
            oButtonSubmit.CssClass = "ms-ButtonHeightWidth";
            this.Controls.Add(oButtonSubmit);

            oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);
        }
        void oButtonSubmit_Click(object sender, EventArgs e)
        {
            if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")
            {
                   SPUtility.TransferToSuccessPage("You must specify a value for this required field");
                //oLabelMessage.Text = "You must specify a value for this required field";
            }
            else
            {
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = SPContext.Current.Web;
                SPList myList = myWeb.Lists["Custom List"];
                SPListItem myListItem = myList.Items.Add();
                myListItem["Title"] = oTextTitle.Text.ToString();
                myListItem["Employee Name"] = oTextName.Text.ToString();
                myListItem["Designation"] = oTextDesignation.Text.ToString();
                myWeb.AllowUnsafeUpdates = true;
                myListItem.Update();
                myWeb.AllowUnsafeUpdates = false;
                oTextTitle.Text = String.Empty;
                oTextName.Text = String.Empty;
                oTextDesignation.Text = String.Empty;
                SPUtility.TransferToSuccessPage("Recorded inserted");
                //oLabelMessage.Text = "Recorded inserted";

            }
        }
    }
}




No comments:

Post a Comment