Sunday, June 1, 2014

SP webpart Examples

WPEX1 : Display contact Details

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 WPEX1.ContactUs
{
    [ToolboxItemAttribute(false)]
    public class ContactUs : WebPart
    {
        protected override void RenderContents
            (HtmlTextWriter writer)
        {
            writer.WriteLine(
                "<h1 style='color:navy;font-size:12pt;'>Peers</h1>");
            writer.WriteLine(
                "<h2 style='font-size:10pt;'>Ameerpet, Hyd -38</h2>");
        }
    }
}

Ex 2 : Display links to open all lsits and libraries

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 WPEX2.ListsLinks
{
    [ToolboxItemAttribute(false)]
    public class ListsLinks : WebPart
    {
        protected override void RenderContents
            (HtmlTextWriter writer)
        {
            SPWeb web = SPContext.Current.Web;
            foreach (SPList list in web.Lists)
                writer.WriteLine(
                    "<a href= '" +
                    list.DefaultViewUrl +
                    "'>" +
                    list.Title +
                    "</a><br/>");

        }
    }
}

WPEX3 : Calculate Interest to be paid on a loan taken

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 MBWPEX3.InterestCalculator
{
    [ToolboxItemAttribute(false)]
    public class InterestCalculator : WebPart
    {
        TextBox txtAmount, txtRate, txtPeriod, txtInterest;
        Button btnCalculateInterest;
        protected override void CreateChildControls()
        {
            txtAmount = new TextBox();
            Controls.Add(txtAmount);
            txtRate = new TextBox();
            Controls.Add(txtRate);
            txtPeriod= new TextBox();
            Controls.Add(txtPeriod);
            txtInterest= new TextBox();
            txtInterest.ReadOnly = true;
            Controls.Add(txtInterest);
            btnCalculateInterest = new Button();
            btnCalculateInterest.Text = "Calculate Interest";
            Controls.Add(btnCalculateInterest);
            btnCalculateInterest.Click +=
                btnCalculateInterest_Click;

        }

        void btnCalculateInterest_Click
            (object sender, EventArgs e)
        {
            double amount, rate, period, interest;
            amount = Convert.ToDouble(txtAmount.Text);
            rate = Convert.ToDouble(txtRate.Text);
            period = Convert.ToDouble(txtPeriod.Text);
            interest = amount * rate * period / 100;
            txtInterest.Text = interest.ToString();
        }
        protected override void RenderContents
            (HtmlTextWriter writer)
        {
            writer.WriteLine("<table>");

            writer.WriteLine("<tr>");
            writer.WriteLine("<td>Amount</td>");
            writer.WriteLine("<td>");
            txtAmount.RenderControl(writer);
            writer.WriteLine("</td>");
            writer.WriteLine("</tr>");

            writer.WriteLine("<tr>");
            writer.WriteLine("<td>Rate</td>");
            writer.WriteLine("<td>");
            txtRate.RenderControl(writer);
            writer.WriteLine("</td>");
            writer.WriteLine("</tr>");

            writer.WriteLine("<tr>");
            writer.WriteLine("<td>Period</td>");
            writer.WriteLine("<td>");
            txtPeriod.RenderControl(writer);
            writer.WriteLine("</td>");
            writer.WriteLine("</tr>");

            writer.WriteLine("<tr>");
            writer.WriteLine("<td>Interest</td>");
            writer.WriteLine("<td>");
            txtInterest.RenderControl(writer);
            writer.WriteLine("</td>");
            writer.WriteLine("</tr>");

            writer.WriteLine("</table>");
            btnCalculateInterest.RenderControl(writer);

        }
    }
}

No comments:

Post a Comment