Search This Blog

Saturday, September 5, 2015

Creating a Web Part Project(Using Webpart(Dynamic Creation))





using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace EUDGridView.ContactFormWebPart
{
    [ToolboxItemAttribute(false)]
    public class ContactFormWebPart : WebPart
    {
        TextBox txtContactName;
        TextBox txtEmailAddress;
        TextBox txtPhone;
        TextBox txtMessage;
        Button btnSendMessage;
        Label lblMessageSent;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Table t;
            TableRow tr;
            TableCell tc;

            // A table that is used to layout the controls
            t = new Table();
                         
            // Label with instructions for the user
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblInstructions = new Label();
            lblInstructions.Text = "Please enter your contact" +
                                   " information and message below.";
            tc.Controls.Add(lblInstructions);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Contact Name label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblContactName = new Label();
            lblContactName.Text = "Name:";
            tc.Controls.Add(lblContactName);
            tr.Controls.Add(tc);

            // Contact Name textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtContactName = new TextBox();
            txtContactName.ID = "txtContactName";
            txtContactName.Width = Unit.Pixel(300);
            tc.Controls.Add(txtContactName);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Email Address label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblEmailAddress = new Label();
            lblEmailAddress.Text = "Email Address:";
            tc.Controls.Add(lblEmailAddress);
            tr.Controls.Add(tc);

            // Email Address textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtEmailAddress = new TextBox();
            txtEmailAddress.ID = "txtEmailAddress";
            txtEmailAddress.Width = Unit.Pixel(300);
            tc.Controls.Add(txtEmailAddress);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Phone Number label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblPhone = new Label();
            lblPhone.Text = "Phone Number:";
            tc.Controls.Add(lblPhone);
            tr.Controls.Add(tc);

            // Phone Number textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtPhone = new TextBox();
            txtPhone.ID = "txtPhone";
            txtPhone.Width = Unit.Pixel(300);
            tc.Controls.Add(txtPhone);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Message label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblMessage = new Label();
            lblMessage.Text = "Message:";
            tc.Controls.Add(lblMessage);
            tr.Controls.Add(tc);

            // Message textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtMessage = new TextBox();
            txtMessage.ID = "txtMessage";
            txtMessage.Height = Unit.Pixel(100);
            txtMessage.Width = Unit.Pixel(400);
            txtMessage.TextMode = TextBoxMode.MultiLine;
            txtMessage.Wrap = true;
            tc.Controls.Add(txtMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Label for telling the user the message was sent
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            lblMessageSent = new Label();
            lblMessageSent.Text = "Your message has been sent. Thank you.";
            lblMessageSent.Font.Bold = true;
            lblMessageSent.Visible = false;
            tc.Controls.Add(lblMessageSent);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Send Message button
            tc = new TableCell();
            btnSendMessage = new Button();
            btnSendMessage.Text = "Send Message";
            btnSendMessage.Click += new EventHandler(btnSendMessage_Click);
            tc.Controls.Add(btnSendMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            this.Controls.Add(t);
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            // Build the email subject string
            System.Text.StringBuilder subject = new System.Text.StringBuilder();
            subject.Append("Contact Form Message from ");
            subject.Append(txtContactName.Text);

            // Build the email message string
            System.Text.StringBuilder message = new System.Text.StringBuilder();
            message.Append("Contact Name: ");
            message.AppendLine(txtContactName.Text);
            message.Append("Email Address: ");
            message.AppendLine(txtEmailAddress.Text);
            message.Append("Phone: ");
            message.AppendLine(txtPhone.Text);
            message.AppendLine();
            message.AppendLine("Message:");
            message.AppendLine(txtMessage.Text);

            // Construct the message header
            System.Collections.Specialized.StringDictionary messageHeader =
            new System.Collections.Specialized.StringDictionary();
            // TODO: Where to send the email
            messageHeader.Add("to", "CustomerService@example.com");
            // TODO: Who the email should be "from"
            messageHeader.Add("from", "ContactForm@example.com");
            messageHeader.Add("subject", subject.ToString());
            messageHeader.Add("content-type", "text/plain");

            // Send the email
            Microsoft.SharePoint.Utilities.SPUtility.SendEmail(
            SPContext.Current.Web, messageHeader, message.ToString());

            // Let the user know the message was sent
            lblMessageSent.Visible = true;

            // Clear out the input fields
            txtContactName.Text = "";
            txtEmailAddress.Text = "";
            txtPhone.Text = "";
            txtMessage.Text = "";
        }
    }
}



No comments:

Post a Comment