Tuesday, 3 January 2012

Web Part Page Property in SharePoint

our new blog site : http://blog.hintechnology.com
Create Custom SharePoint Web Part Property.
In this example i have created custom page viewer Web Part
1. I have created  a Visual Web Part, and have added the following in this Web Part Pages.

   a) Add the following Code in the WebPart.cs File of the Visual Web Part as Displayed below

[ToolboxItemAttribute(false)]
    public class CustomPageViewerWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/CustomPageViewer/CustomPageViewerWebPart/CustomPageViewerWebPartUserControl.ascx";

        [WebBrowsable(true),
         Category("Miscellaneous"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayName("Enter URL")]
        public string PageURL { get; set; }

        [WebBrowsable(true),
         Category("Miscellaneous"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayName("PageHeight")]
        public string PageHeight { get; set; }

        [WebBrowsable(true),
         Category("Miscellaneous"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayName("PageWidth")]
        public string PageWidth { get; set; }


        protected override void CreateChildControls()
        {
            CustomPageViewerWebPartUserControl control = Page.LoadControl(_ascxPath) as      CustomPageViewerWebPartUserControl;
            Controls.Add(control);

            if (control != null)
                control.CustomPVWebPart = this;
            Controls.Add(control);
        }
    }



  b) Add the Following Design in the User Control Page


<div id="divIframe" visible="false" runat="server">
    <iframe src="http://www.google.com" width="100%" height="100%" runat="server" id="customiframe">
    </iframe>
</div>
<div id="divMessage" visible="true" runat="server">
    <asp:Label ID="lblMessage" runat="server" Text=""> </asp:Label>
</div>



    c) Add the following code in the Code Behind file of the Web Part 

   

    public partial class CustomPageViewerWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public CustomPageViewerWebPart CustomPVWebPart { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            try
            {
                if (this.CustomPVWebPart != null && this.CustomPVWebPart.PageURL != null
                    && this.CustomPVWebPart.PageHeight != null && this.CustomPVWebPart.PageWidth != null)
                {
                    divIframe.Visible = true;
                    divMessage.Visible = false;
                    customiframe.Attributes.Add("src", this.CustomPVWebPart.PageURL);
                    customiframe.Attributes.Add("width", this.CustomPVWebPart.PageWidth);
                    customiframe.Attributes.Add("height", this.CustomPVWebPart.PageHeight);
                }
                else
                {
                    divIframe.Visible = false;
                    divMessage.Visible = true;
                    lblMessage.Text = "no page is referred";
                }
            }
            catch (Exception ex)
            {
                divIframe.Visible = false;
                divMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
        }
    }



OutPut



Thanks for Reading this Blog

No comments:

Post a Comment