Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Wednesday, March 27, 2013

How to Display Ads in Visual WebPart with Ajax Timer Control And Asp.Net Image.



Note:-
        We are Retrieving Data From SharePoint Picture Library.
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AjaxAdRotatorUserControl.ascx.cs" Inherits="AjaxAdRotator.AjaxAdRotator.AjaxAdRotatorUserControl" %>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Image ID="imgRotator" runat="server" Width="100px" Height="100px" />
        <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
        </asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>
Ascx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Collections;

namespace AjaxAdRotator.AjaxAdRotator
{
    public partial class AjaxAdRotatorUserControl : UserControl
    {
        String[] strAllImages;
        String[] strAllImagesTitle;
        protected void Page_Load(object sender, EventArgs e)
        {
            getImages();
        }

        public void getImages()
        {
            try
            {
                SPListItemCollection myColl = SPContext.Current.Web.Lists["Rotating Images"].Items;
                if (myColl.Count > 0)
                {
                    strAllImages = new String[myColl.Count];
                    strAllImagesTitle = new String[myColl.Count];
                    for (int j = 0; j < myColl.Count; j++)
                    {
                        SPListItem item = myColl[j];
                        strAllImages[j] = item["EncodedAbsUrl"].ToString();
                        strAllImagesTitle[j] = item["BaseName"].ToString();
                    }
                }
            }
            catch (Exception Ex)
            {
                ;
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
                Random r = new Random();
                imgRotator.ImageUrl = strAllImages[r.Next(0, strAllImages.Length)];
                imgRotator.ToolTip = strAllImagesTitle[r.Next(0, strAllImagesTitle.Length)];
        }

      
    }
}

No comments:

Post a Comment