(Dynamic Responsive jQuery Carousel in Asp.net) – Khi cần đưa những thông tin mới, những sự kiện quan trọng, những sản phẩm mới đến người xem người phát triển thường sử dụng Slider hoặc Carousel. Bài viết dưới đây sẽ hướng dẫn các bạn cách sử dụng thư viện Elastislide kết hợp với Repeater để tạo Carousel.
- B1: Tạo Project trong Microsoft Visual Studio 2010
- B2: Download thư viện Elastislide tại đây, copy file jquery.elastislide.js, jquerypp.custom.js, modernizr.custom.17475.js vào thư mục Js
- B3: Copy file elastislide.css vào thư mục Styles
- B5: Mở file Site.Master dạng HTML và bổ xung đoạn mã phía dưới trong thẻ Head
<head runat="server">
<title>Responsive Image Carousel with jQuery in Asp.net</title>
<link href="~/Styles/Site.css"rel="stylesheet"type="text/css"/>
<link rel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<link href="Styles/elastislide.css"rel="stylesheet"type="text/css"/>
<script type="text/javascript"src="Js/modernizr.custom.17475.js"></script>
<asp:ContentPlaceHolderID="HeadContent"runat="server">
</asp:ContentPlaceHolder>
</head>
- B6: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
<%@ PageTitle="Responsive Bootstrap Carousel in Repeater Asp.net" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ResponsiveImageCarousel._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Dynamic Responsive Image Carousel with jQuery in Asp.net</h4>
</div>
<div class="panel-body">
<ul id="carousel"class="elastislide-list">
<asp:Repeater ID="rptObject"runat="server">
<ItemTemplate>
<li>
<a href='<%# Eval("URL")%>'><img src='<%# Eval("ImageURL")%>' alt='<%# Eval("CarouselName")%>' /></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</div>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript"src="js/jquerypp.custom.js"></script>
<script type="text/javascript"src="js/jquery.elastislide.js"></script>
<script type="text/javascript">
$('#carousel').elastislide();
</script>
</asp:Content>
- B7: 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.Web.UI;
using System.Web.UI.WebControls;
namespace ResponsiveImageCarousel
{
public partial class _Default : System.Web.UI.Page
{
#region"Bind Data"
private voidBindData()
{
DataTable objBind = newDataTable();
objBind.Columns.Add("CarouselName", typeof(string));
objBind.Columns.Add("URL", typeof(string));
objBind.Columns.Add("ImageURL", typeof(string));
objBind.Rows.Add("Carousel1", "", "Images/small/1.jpg");
objBind.Rows.Add("Carousel2", "", "Images/small/2.jpg");
objBind.Rows.Add("Carousel3", "", "Images/small/3.jpg");
objBind.Rows.Add("Carousel4", "", "Images/small/4.jpg");
objBind.Rows.Add("Carousel5", "", "Images/small/5.jpg");
objBind.Rows.Add("Carousel6", "", "Images/small/6.jpg");
objBind.Rows.Add("Carousel7", "", "Images/small/7.jpg");
objBind.Rows.Add("Carousel8", "", "Images/small/8.jpg");
objBind.Rows.Add("Carousel9", "", "Images/small/9.jpg");
objBind.Rows.Add("Carousel10", "", "Images/small/10.jpg");
objBind.Rows.Add("Carousel11", "", "Images/small/11.jpg");
objBind.Rows.Add("Carousel12", "", "Images/small/12.jpg");
objBind.Rows.Add("Carousel13", "", "Images/small/13.jpg");
objBind.Rows.Add("Carousel14", "", "Images/small/14.jpg");
objBind.Rows.Add("Carousel15", "", "Images/small/15.jpg");
rptObject.DataSource = objBind;
rptObject.DataBind();
}
#endregion
#region "Event Handles"
protected voidPage_Load(object sender, System.EventArgs e)
{
try
{
if (!IsPostBack)
{
BindData();
}
}
catch
{
}
}
#endregion
}
}
VB.NET Code
'Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
Namespace ResponsiveImageCarousel
Public Class _Default
Inherits System.Web.UI.Page
#Region "Bind Data"
Private SubBindData()
Dim objBind As New DataTable
With objBind.Columns
.Add("CarouselName", GetType(String))
.Add("URL", GetType(String))
.Add("ImageURL", GetType(String))
End With
objBind.Rows.Add("Carousel1", "", "Images/small/1.jpg")
objBind.Rows.Add("Carousel2", "", "Images/small/2.jpg")
objBind.Rows.Add("Carousel3", "", "Images/small/3.jpg")
objBind.Rows.Add("Carousel4", "", "Images/small/4.jpg")
objBind.Rows.Add("Carousel5", "", "Images/small/5.jpg")
objBind.Rows.Add("Carousel6", "", "Images/small/6.jpg")
objBind.Rows.Add("Carousel7", "", "Images/small/7.jpg")
objBind.Rows.Add("Carousel8", "", "Images/small/8.jpg")
objBind.Rows.Add("Carousel9", "", "Images/small/9.jpg")
objBind.Rows.Add("Carousel10", "", "Images/small/10.jpg")
objBind.Rows.Add("Carousel11", "", "Images/small/11.jpg")
objBind.Rows.Add("Carousel12", "", "Images/small/12.jpg")
objBind.Rows.Add("Carousel13", "", "Images/small/13.jpg")
objBind.Rows.Add("Carousel14", "", "Images/small/14.jpg")
objBind.Rows.Add("Carousel15", "", "Images/small/15.jpg")
rptObject.DataSource = objBind
rptObject.DataBind()
End Sub
#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
BindData()
End If
Catch ex As Exception
End Try
End Sub
#End Region
End Class
End Namespace
Chúc các bạn thành công!
Quang Bình
0 comments Blogger 0 Facebook
Post a Comment