Bootstrap List Group with Badges in Repeater ASP.Net
(Bootstrap List Group with Badges in ASP.Net) – Bài viết dưới đây sẽ hướng dẫn cách sử dụng Bootstrap List Group kết hợp với Badges và Repeater ASP.Net để tạo danh sách. Danh sách được lấy từ bảng Categories trong CSDL Northwind, chương trình sẽ sử dụng Badges để hiển thị số liệu thống kê tổng số sản phẩm theo từng loại.
- B1: Download CSDL Northwind tại đây và thực hiện công việc Restore Data.
- B1: Download CSDL Northwind tại đây và thực hiện công việc Restore Data.
- B2: 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 BootstrapListGroups
{
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 DataTable FillTable(string sql)
{
try
{
DataTable tb = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter(sql, _connectionString);
adap.Fill(tb);
return tb;
}
catch
{
return null;
}
}
#endregion
}
}
VB.NET Code
Imports System.Data.SqlClient
Imports System.Data
Namespace BootstrapListGroups
Public Class SqlDataProvider
#Region "Membres Prives"
Shared _IsError As Boolean = False
Private _connectionString As String
#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() As String
Get
Return _connectionString
End Get
End Property
#End Region
#Region "Functions"
Public Function FillTable(ByVal sql As String) As DataTable
Try
Dim tb As New DataTable
Dim adap As New SqlDataAdapter(sql, _connectionString)
adap.Fill(tb)
Return tb
Catch ex As Exception
Return Nothing
End Try
End Function
#End Region
End Class
Chú ý: Thuộc tính SiteSqlServer chính là chuỗi Connect với SQL Server trong file Web.Config
- B3: 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>Bootstrap List Group with Badges in Repeater
ASP.Net</title>
<link href="~/Styles/Site.css"
rel="stylesheet"
type="text/css"
/>
<link rel="stylesheet"
href="https://netdna.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"
/>
<script type="text/javascript"
src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript"
src="http://netdna.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<asp:ContentPlaceHolder
ID="HeadContent"
runat="server">
</asp:ContentPlaceHolder>
</head>
- B4: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
C# Code
<%@ Page
Title="Bootstrap
List Group with Badges in Repeater ASP.Net" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="BootstrapListGroups._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>Bootstrap List Group with Badges in Repeater
ASP.Net</h4>
</div>
<div class="panel-body">
<div class="row">
<div
class="col-lg-3">
<div
class="list-group">
<asp:Repeater ID="rptList" OnItemDataBound="rptList_ItemDataBound" runat="server">
<ItemTemplate>
<a id="AnchorItem" runat="server" class="list-group-item"><%# Eval("CategoryName")%></a>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</div>
</div>
</div>
</asp:Content>
- B5: 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.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BootstrapListGroups
{
public partial class _Default :
System.Web.UI.Page
{
#region
"Repeater Methods"
protected void
rptList_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item
| e.Item.ItemType == ListItemType.AlternatingItem)
{
int TotalProducts =Convert.ToInt32(DataBinder.Eval(e.Item.DataItem,
"TotalProducts"));
HtmlAnchor AnchorItem = (HtmlAnchor)e.Item.FindControl("AnchorItem");
if ((AnchorItem != null))
{
AnchorItem.Controls.Add(new LiteralControl("<span
class=\"badge\">"));
AnchorItem.Controls.Add(new LiteralControl(TotalProducts.ToString()));
AnchorItem.Controls.Add(new LiteralControl("</span>"));
}
}
}
#endregion
#region "Bind Data"
private void
BindData()
{
SqlDataProvider objSQL = new
SqlDataProvider();
DataTable objBind = default(DataTable);
//Caching
if (Cache["Cache_Categories"]
== null)
{
objBind = objSQL.FillTable("SELECT Products.CategoryID,
Categories.CategoryName, COUNT(Products.ProductID) AS TotalProducts FROM
Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Products.CategoryID, Categories.CategoryName ORDER BY
Categories.CategoryName");
Cache["Cache_Categories"] =
objBind;
}
else
{
objBind = (DataTable)Cache["Cache_Categories"];
}
if (objBind != null)
{
if (objBind.Rows.Count > 0)
{
rptList.DataSource = objBind;
rptList.DataBind();
}
}
}
#endregion
#region
"Event Handles"
protected void
Page_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
Imports System.IO
Namespace BootstrapListGroups
Public Class _Default
Inherits System.Web.UI.Page
#Region "Repeater
Methods"
Private Sub
rptList_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs)
Handles rptList.ItemDataBound
If (e.Item.ItemType = ListItemType.Item
Or e.Item.ItemType = ListItemType.AlternatingItem)
Then
Dim TotalProducts As Integer = DataBinder.Eval(e.Item.DataItem, "TotalProducts")
Dim AnchorItem As
HtmlAnchor = DirectCast(e.Item.FindControl("AnchorItem"), HtmlAnchor)
If Not
AnchorItem Is Nothing
Then
AnchorItem.Controls.Add(New LiteralControl("<span
class=""badge"">"))
AnchorItem.Controls.Add(New LiteralControl(TotalProducts))
AnchorItem.Controls.Add(New LiteralControl("</span>"))
End If
End If
End Sub
#End Region
#Region "Bind Data"
Private Sub
BindData()
Dim objSQL As New SqlDataProvider
Dim objBind As DataTable
'Caching
If Cache("Cache_Categories")
Is Nothing Then
objBind = objSQL.FillTable("SELECT Products.CategoryID,
Categories.CategoryName, COUNT(Products.ProductID) AS TotalProducts FROM
Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Products.CategoryID, Categories.CategoryName ORDER BY
Categories.CategoryName")
Cache("Cache_Categories") =
objBind
Else
objBind = CType(Cache("Cache_Categories"),
DataTable)
End If
If Not objBind Is Nothing Then
If objBind.Rows.Count > 0 Then
rptList.DataSource = objBind
rptList.DataBind()
End If
End If
End Sub
#End Region
#Region "Event
Handles"
Protected Sub
Page_Load(ByVal sender As
Object, 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
No Comment to " Bootstrap List Group with Badges in Repeater ASP.Net "