Thursday, August 17, 2017

Asp.net Adrotator : Display ads without page refresh

In this article I am going to explain how to show ads without page refresh using Sql server database in asp.net Adrotator control.

Description:
Adrotator control is used to show custom Ads (advertisements) on asp.net build websites. When users click on any ad they will be redirect to link assign for that. We can use both Xml and database as data source. Xml and database table must have following properties and columns: ImageUrl, NavigateUrl, AlternateText, Impressions, Keyword, Width and Height.

Implementations:
In this example I am going to use sql server database as adrotator data source. I have create a table Ads and insert some dummy records into it. To show ads without page refresh I am going to use Updatepanel and Timer control.

Asp.net Adrotator : Display ads without page refresh


Complete source of webform

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
     <asp:Timer ID="Timer1" Interval="1000" runat="server"/>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                 <asp:AdRotator ID="AdRotator1" runat="server" Target="_blank"/>
            </ContentTemplate>
            <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                        </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Add namespaces
C# Code
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

VB.net Code
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Bind data to Adrotator
Create a method to get data from database and call it on page load.

C# Code
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AdRotator1.DataSource = BindAds();
            AdRotator1.DataBind();
        }
    }
    public DataTable BindAds()
    {
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Ads", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            return dt;
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        AdRotator1.DataSource = BindAds();
        AdRotator1.DataBind();
    }


VB.net Code
  Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            AdRotator1.DataSource = BindAds()
            AdRotator1.DataBind()
        End If
    End Sub
   Public Function BindAds() As DataTable
        Dim adp As New SqlDataAdapter("Select * from Ads", con)
        Dim dt As New DataTable()
        adp.Fill(dt)
        Return dt
    End Function

    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        AdRotator1.DataSource = BindAds()
        AdRotator1.DataBind()
    End Sub

No comments:

Post a Comment