salutation
voila , je travaille sur un projet sharepoint 2010 , et je souhaite réaliser une web part qui permet de compter le nombre de clics sur une image publicitaire] et de le stocker dans une base de donnees avec son ID ( ID de l image clique )
voila le code que j ai mis :
CODE 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="ClickCounterUserControl.ascx.cs"
Inherits="FormBaseAuthentification.ClickCounter.ClickCounterUserControl" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl ="http://www.guillaumemenant.fr/wp-content/photos/nd400/20100324-003.jpg"
onclick="ImageButton1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
CODE CS
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using Microsoft.SharePoint;
namespace FormBaseAuthentification.ClickCounter
{
public partial class ClickCounterUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
SPUser user = SPContext.Current.Web.CurrentUser;
SqlConnection cn = new SqlConnection("Data source=.;Initial Catalog=Inscription;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select IdUser from dbo.db_Users where UserName ='" + user.Name + "';");
cn.Open();
cmd.Connection = cn;
SqlDataReader dr = cmd.ExecuteReader();
int id = int.MinValue;
if (dr.HasRows)
{
while (dr.Read())
{
id = Convert.ToInt32(dr["IdUser"]); //dr.GetInt32(0);
}
}
dr.Close();
if (id != int.MinValue)
{
SqlCommand cmd2 = new SqlCommand("insert into ClickCounter(DateClick,IdUser) VALUES ('" + DateTime.Now + "'," + id + " )");
cmd2.Connection = cn;
cmd2.ExecuteNonQuery();
}
cn.Close();
}
}
}
Note
------
l image peut etre modifier a n importe quel moment
merci pour votre aide
Partager