(jQuery Slide trong Asp.net) - Để tạo Slide có rất nhiều công cụ để làm việc này, tùy vào từng công việc, từng yêu cầu mà người lập trình sẽ lựa chọn cho phù hợp. Hôm nay Thủ thuật lập trình sẽ giới thiệu với các bạn cách sử dụng jQuery để tạo Slide.  Thông tin Slide được lấy từ bảng dữ liệu trong CSDL SQL như: Tên, mô tả, đường dẫn và tên ảnh.

Nghe những bài hát đỉnh nhất về Thấy cô giáo - Nghe trên Youtube




Code Example C#, Code Example VB.NET
Code Example C#, Code Example VB.NET




- B1: Tạo CSDL SQL SlideShow

B2: Tạo Bảng SlideShows có cấu trúc phía dưới trong CSDL SQL Server.

STTTên trườngKiểu trườngGhi chú
1ItemIDIntTrường tự tăng
2Titlenvarchar(250)
3Descriptionnvarchar(500)Mô tả
4ImageURLnvarchar(350)
5SortOrderIntThứ tự sắp xếp
6IsVisiblebit


B3: Nhập dữ liệu cho bảng SlideShows 

B4: Tạo stored procedure trong SQL Server

CREATE      procedure [dbo].[Pro_SlideShow_List]
      @IsVisible bit
as

declare @strSQL   nvarchar(1000)
declare @strWhere nvarchar(500)
declare @strOrder nvarchar (50)

set @strSQL= 'Select * from SlideShows'
set @strWhere =' where 1=1'
if @IsVisible=0
      set @strWhere= @strWhere  +' and (IsVisible=0 Or IsVisible Is Null)'

set @strOrder =' Order by SortOrder, Title'

set @strSQL=@strSQL+@strWhere+@strOrder
print @strSQL

exec sp_executesql @strSQL

Bạn có thể tải về bảng cơ sở dữ liệu SQL bằng cách nhấn vào liên kết tải về dưới đây

B5: Tạo Project trong Microsoft Visual Studio 2010

Trong Visual Studio tạo 1 Class có tên: Utility và nhập đoạn Code phía dưới cho Class này.

C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace jQuerySlidePanel
{
    public class SqlDataProvider
    {
        #region"Membres Prives"

        private string_connectionString;

        #endregion

        #region"Constructeurs"

        public SqlDataProvider()
        {
            try
            {
                _connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
            }
            catch
            {
            }
        }

        #endregion

        #region"Proprietes"

        public string ConnectionString
        {
            get { return_connectionString; }
        }

        #endregion

        #region"Functions"

        public DataTableFillTable(string ProcName, params ObjectPara[] Para)
        {
            try
            {
                DataTable tb = new DataTable();
                SqlDataAdapter adap = new SqlDataAdapter(ProcName, _connectionString);
                adap.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (Para != null)
                {
                    foreach (ObjectParap in Para)
                    {
                        adap.SelectCommand.Parameters.Add(new SqlParameter(p.Name, p.Value));
                    }
                }
                adap.Fill(tb);
                return tb;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }

    public class ObjectPara
    {
        string _name;

        object _Value;
        public ObjectPara(stringPname, object PValue)
        {
            _name = Pname;
            _Value = PValue;
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public object Value
        {
            get { return _Value; }
            set { _Value = value; }
        }
    }
}

VB.NET Code
Imports System.Data.SqlClient
Imports System.Data

Namespace jQuerySlidePanel

    Public Class SqlDataProvider

#Region "Membres Prives"

        Shared _IsError As Boolean = False
        Private _connectionString AsString

#End Region

#Region "Constructeurs"

        Public Sub New()
            Try
 _connectionString = ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString
                _IsError = False
            Catch ex As Exception
                _IsError = True
            End Try
        End Sub

#End Region

#Region "Proprietes"

        Public ReadOnly Property ConnectionString() AsString
            Get
                Return _connectionString
            End Get
        End Property

#End Region

#Region "Functions"

        Public FunctionFillTable(ByVal sql AsString) As DataTable
            Try
                Dim tb AsNew DataTable
                Dim adap AsNew SqlDataAdapter(sql, _connectionString)
                adap.Fill(tb)
                Return tb
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        Public FunctionFillTable(ByVal ProcName As String, ByVal ParamArrayPara() As ObjectPara) As DataTable
            Try
                Dim tb AsNew DataTable
                Dim adap AsNew SqlDataAdapter(ProcName, _connectionString)
                adap.SelectCommand.CommandType = CommandType.StoredProcedure
                If NotPara Is NothingThen
                    For Eachp As ObjectParaIn Para
                        adap.SelectCommand.Parameters.Add(New SqlParameter(p.Name, p.Value))
                    Next
                End If
                adap.Fill(tb)
                Return tb
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

#End Region

    End Class

    Public Class ObjectPara
        Dim _name As String
        Dim _Value As Object

        Sub New(ByVal Pname As String, ByVal PValue As Object)
            _name = Pname
            _Value = PValue
        End Sub

        Public PropertyName() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Public PropertyValue() As Object
            Get
                Return _Value
            End Get
            Set(ByVal value As Object)
                _Value = value
            End Set
        End Property

    End Class

End Namespace


Chú ý: Thuộc tính SiteSqlServer chính là chuỗi Connect với SQL Server trong file Web.Config

- B6: Download thư viện ảnh tại đây và Copy chúng vào thư mục Images của Project

- B7: Mở file Site.Master dạng HTML và bổ xung đoạn mã phía dưới trong thẻ Head
<head id="Head1" runat="server">
    <title>Dynamic jQuery Slide Panel</title>
    <link href="~/Styles/Site.css"rel="stylesheet"type="text/css"/>
    <link href="~/Styles/featured_slide.css"rel="stylesheet"type="text/css"/>
    <link href="~/Styles/Slidepanel.css"rel="stylesheet"type="text/css"/>
    <script type="text/javascript"src="/Js/jquery-1.4.1.min.js"></script>
    <script type="text/javascript"src="/Js/jquery.slidepanel.setup.js"></script>
    <script type="text/javascript"src="/Js/jquery.cycle.min.js"></script>
    <script type="text/javascript"src="/Js/jquery.cycle.setup.js"></script>
    <asp:ContentPlaceHolderID="HeadContent"runat="server">
    </asp:ContentPlaceHolder>
</head>

B8: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
<%@ PageTitle="Dynamic jQuery Slide Panel" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="jQuerySlidePanel._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div style="background-color:#423739;">
        <asp:Literal ID="ltSlide"runat="server"></asp:Literal>
    </div>
</asp:Content>

B9: Viết Code cho file Default.aspx

C# Code
//Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Web.UI;

namespace jQuerySlidePanel
{
    public partial class _Default : System.Web.UI.Page
    {

        #region"Create SlidePanel"

        private voidCreateSlide()
        {
            DataTable objBind = newDataTable();
            string sSlide = "";
            string sTitle = "";
            string ImagePath = "";

            objBind = BindData();

            if (objBind != null)
            {
                if (objBind.Rows.Count > 0)
                {
                    sSlide += "<div class=\"wrapper col2\">";
                    sSlide += "  <div id=\"featured_slide\">";
                    foreach (DataRowrow in objBind.Rows)
                    {
                        var _with1 = row;
                        sTitle = row["Title"].ToString();
                        ImagePath = row["ImageURL"].ToString();
                        if (File.Exists(MapPath(ImagePath)))
                        {
                            ImagePath = Page.ResolveUrl(ImagePath);
                        }
                        sSlide += "             <div class=\"featured_box\">";
                        sSlide += "<img alt=\"" + sTitle + "\" src=\"" + ImagePath + "\" />";
                        sSlide += "             </div>";
                    }
                    sSlide += "     </div>";
                    sSlide += "</div>";
                }
            }
            ltSlide.Text = sSlide;
        }

        #endregion

        #region"Bind Data"

        private DataTableBindData()
        {
            SqlDataProvider objSQL = newSqlDataProvider();
            DataTable objBind = objSQL.FillTable("Pro_SlideShow_List", new ObjectPara("@IsVisible", 1));
            return objBind;
        }

        #endregion

        #region"Event Handles"

        protected voidPage_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    CreateSlide();
                }
            }
            catch
            {
            }
        }

        #endregion
    }
}

VB.NET Code
'Visit http://thuthuatlaptrinh.blogspot.com for more ASP.NET Tutorials
Imports System.IO

Namespace jQuerySlidePanel

    Public Class _Default
        Inherits System.Web.UI.Page

#Region "Create SlidePanel"

        Private SubCreateSlide()
            Dim objBind As New DataTable
            Dim sSlide As String = ""
            Dim sTitle As String = ""
            Dim ImagePath As String = ""

            objBind = BindData()

            If Not objBind Is Nothing Then
                If objBind.Rows.Count > 0 Then
                    sSlide &= "<div class=""wrapper col2"">" & vbCrLf
                    sSlide &= "  <div id=""featured_slide"">" & vbCrLf
                    For Eachrow As DataRowIn objBind.Rows
                        With row
                            sTitle = row("Title").ToString
                            ImagePath = row("ImageURL").ToString
                            If File.Exists(MapPath(ImagePath)) Then
                                ImagePath = Page.ResolveUrl(ImagePath)
                            End If
                            sSlide &= "             <div class=""featured_box"">"& vbCrLf
                            sSlide &= "<img alt=""" & sTitle & """ src="""& ImagePath & """ />"
                            sSlide &= "             </div>" & vbCrLf
                        End With
                    Next
                    sSlide &= "     </div>" & vbCrLf
                    sSlide &= "</div>"& vbCrLf
                End If
            End If
            ltSlide.Text = sSlide
        End Sub

#End Region

#Region "Bind Data"

        Private FunctionBindData() As DataTable
            Dim objSQL As New SqlDataProvider
            Dim objBind As DataTable = objSQL.FillTable("Pro_SlideShow_List", New ObjectPara("@IsVisible", 1))
            Return objBind
        End Function

#End Region

#Region "Event Handles"

        Protected SubPage_Load(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Page.IsPostBack = False Then
                    CreateSlide()
                End If
            Catch ex As Exception

            End Try
        End Sub

#End Region

    End Class

End Namespace

Code Example C#, Code Example VB.NET
Code Example C#, Code Example VB.NET




Chúc các bạn thành công!

Quang Bình

0 comments Blogger 0 Facebook

Post a Comment

 
lập trình đốt nét © 2013. All Rights Reserved. Powered by Blogger
Top