(Dynamic Bootstrap Vertical Tabs) – Trong các bài viết trước, thủ thuật lập trình đã giới thiệu với các bạn nhiều cách để tạo Tabs động. Ví dụ như sử dụng jQuery, AjaxControlToolkit, Bootstrap nhưng đó là cách tạo Horizontal Tabs. Hôm nay thủ thuật lập trình sẽ giới thiệu với các bạn cách để tạo Vertical Tabs. Nội dung các Tabs được hiển thị từ các Control ngoài và đường dẫn cũng như tên các Control này được lấy từ CSDL SQL Server.
- B1: Tạo CSDL DynamicTabs trong SQL Server
- B2: Tạo Bảng DynamicTabs có cấu trúc phía dưới.
STT | Tên trường | Kiểu trường | Ghi chú |
1 | ItemID | Int | Trường tự tăng |
2 | TabName | nvarchar(150) | |
3 | Description | nvarchar(250) | Mô tả |
4 | ControlURL | nvarchar(250) | |
5 | ImageURL | nvarchar(250) | |
6 | SortOrder | Int | Thứ tự sắp xếp |
7 | IsVisible | bit | Ân/Hiện Tab |
- B3: Nhập dữ liệu cho bảng DynamicTabs
- B4: Tạo stored procedure trong SQL Server
CREATE procedure [dbo].[Pro_DynamicTabs_List]
@IsVisible bit
as
declare @strSQL nvarchar(1000)
declare @strWhere nvarchar(500)
declare @strOrder nvarchar (50)
set @strSQL= 'Select * from DynamicTabs'
set @strWhere =' where 1=1'
if @IsVisible=1
set @strWhere= @strWhere +' and (IsVisible=1)'
if @IsVisible=0
set @strWhere= @strWhere +' and (IsVisible=0 Or IsVisible Is Null)'
set @strOrder =' Order by SortOrder, TabName'
set @strSQL=@strSQL+@strWhere+@strOrder
print @strSQL
exec sp_executesql @strSQL
- 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 DynamicBootstrapVerticalTabs
{
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 stringConnectionString
{
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; }
}
}
}
Imports System.Data.SqlClient
Imports System.Data
Namespace DynamicBootstrapVerticalTabs
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 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 bootstrap.vertical-tabs.css và Copy vào thư mục Styles của Project
- B7: Mở file Site.Master dạng HTML và bổ xung đoạn mã phía dưới trong thẻ Head
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"/>
<link rel="stylesheet"href="Styles/bootstrap.vertical-tabs.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>
- B8: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
<%@ PageTitle=" Bootstrap Vertical Tabs in ASP.Net" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicBootstrapVerticalTabs._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1"runat="server">
</asp:ScriptManager>
<h1>
Bootstrap Vertical Tabs in ASP.Net
</h1>
<br />
<asp:Panel ID="pnlTabs"runat="server">
</asp:Panel>
</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 DynamicBootstrapVerticalTabs
{
public partial class _Default : System.Web.UI.Page
{
#region"Create Tabs"
private voidCreateTabs()
{
DataTable objBind = newDataTable();
Control myCtrl = newControl();
int i = 1;
string TabName = "";
string ControlURL = "";
objBind = BindData();
pnlTabs.Controls.Clear();
if (objBind != null) {
if (objBind.Rows.Count > 0) {
pnlTabs.Controls.Add(new LiteralControl("<div class=\"col-sm-6\">"));
pnlTabs.Controls.Add(new LiteralControl("<h3>Left Tabs</h3>"));
pnlTabs.Controls.Add(new LiteralControl("<hr/>"));
pnlTabs.Controls.Add(new LiteralControl("<div class=\"col-xs-3\">"));
pnlTabs.Controls.Add(new LiteralControl("<ul class=\"nav nav-tabs tabs-left\">"));
foreach (DataRowrow in objBind.Rows) {
if ((row != null)) {
TabName = row["TabName"].ToString();
if (i == 1) {
pnlTabs.Controls.Add(new LiteralControl("<li class=\"active\"><a href=\"#tab" + i + "\" data-toggle=\"tab\">" + TabName + "</a></li>"));
} else {
pnlTabs.Controls.Add(new LiteralControl("<li><a href=\"#tab" + i + "\" data-toggle=\"tab\">" + TabName + "</a></li>"));
}
}
i = i + 1;
}
pnlTabs.Controls.Add(new LiteralControl("</ul>"));
pnlTabs.Controls.Add(new LiteralControl("</div>"));
i = 1;
pnlTabs.Controls.Add(new LiteralControl("<div class=\"col-xs-9\">"));
pnlTabs.Controls.Add(new LiteralControl("<div class=\"tab-content\">"));
pnlTabs.Controls.Add(new LiteralControl("<!-- Tab panes -->"));
foreach (DataRowrow in objBind.Rows) {
if (row != null) {
ControlURL = row["ControlURL"].ToString();
if (File.Exists(Server.MapPath("~/" + ControlURL))) {
myCtrl = Page.LoadControl(ControlURL);
}
if (i == 1) {
pnlTabs.Controls.Add(new LiteralControl("<div class=\"tab-pane active\" id=\"tab" + i + "\">"));
} else {
pnlTabs.Controls.Add(new LiteralControl("<div class=\"tab-pane\" id=\"tab" + i + "\">"));
}
pnlTabs.Controls.Add(myCtrl);
pnlTabs.Controls.Add(new LiteralControl("</div>"));
}
i = i + 1;
}
pnlTabs.Controls.Add(new LiteralControl("</div>"));
pnlTabs.Controls.Add(new LiteralControl("<div class=\"clearfix\"></div>"));
pnlTabs.Controls.Add(new LiteralControl("</div>"));
}
}
}
private DataTableBindData()
{
SqlDataProvider objSQL = newSqlDataProvider();
DataTable objBind = null;
//Caching
if (Cache["Cache_VerticalTabs"] == null) {
objBind = objSQL.FillTable("Pro_DynamicTabs_List", new ObjectPara("@IsVisible", 1));
Cache["Cache_VerticalTabs"] = objBind;
} else {
objBind = (DataTable)Cache["Cache_VerticalTabs"];
}
return objBind;
}
#endregion
#region"Event Handles"
protected voidPage_Load(object sender, System.EventArgs e)
{
try
{
if (!IsPostBack)
{
CreateTabs();
}
}
catch
{
}
}
#endregion
}
}VB.NET Code
'Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
Imports System.IO
Namespace DynamicBootstrapVerticalTabs
Public Class _Default
Inherits System.Web.UI.Page
#Region "Tabs"
Private SubCreateTabs()
Dim objBind As New DataTable
Dim myCtrl As New Control
Dim i As Integer = 1
Dim TabName As String = ""
Dim ControlURL As String = ""
objBind = BindData()
pnlTabs.Controls.Clear()
If Not objBind Is Nothing Then
If objBind.Rows.Count > 0 Then
pnlTabs.Controls.Add(New LiteralControl("<div class=""col-sm-6"">"))
pnlTabs.Controls.Add(New LiteralControl("<h3>Left Tabs</h3>"))
pnlTabs.Controls.Add(New LiteralControl("<hr/>"))
pnlTabs.Controls.Add(New LiteralControl("<div class=""col-xs-3"">"))
pnlTabs.Controls.Add(New LiteralControl("<ul class=""nav nav-tabs tabs-left"">"))
For Eachrow As DataRowIn objBind.Rows
IfNot row Is Nothing Then
TabName = row("TabName").ToString()
If i = 1 Then
pnlTabs.Controls.Add(New LiteralControl("<li class=""active""><a href=""#tab"& i & """ data-toggle=""tab"">" & TabName & "</a></li>"))
Else
pnlTabs.Controls.Add(New LiteralControl("<li><a href=""#tab" & i & """ data-toggle=""tab"">" & TabName & "</a></li>"))
EndIf
End If
i = i + 1
Next
pnlTabs.Controls.Add(New LiteralControl("</ul>"))
pnlTabs.Controls.Add(New LiteralControl("</div>"))
i = 1
pnlTabs.Controls.Add(New LiteralControl("<div class=""col-xs-9"">"))
pnlTabs.Controls.Add(New LiteralControl("<div class=""tab-content"">"))
pnlTabs.Controls.Add(New LiteralControl("<!-- Tab panes -->"))
For Eachrow As DataRowIn objBind.Rows
If Not row Is Nothing Then
ControlURL = row("ControlURL").ToString()
If File.Exists(Server.MapPath("~/" & ControlURL)) Then
myCtrl = Page.LoadControl(ControlURL)
End If
If i = 1 Then
pnlTabs.Controls.Add(New LiteralControl("<div class=""tab-pane active"" id=""tab"& i & """>"))
Else
pnlTabs.Controls.Add(New LiteralControl("<div class=""tab-pane"" id=""tab" & i & """>"))
End If
pnlTabs.Controls.Add(myCtrl)
pnlTabs.Controls.Add(New LiteralControl("</div>"))
End If
i = i + 1
Next
pnlTabs.Controls.Add(New LiteralControl("</div>"))
pnlTabs.Controls.Add(New LiteralControl("<div class=""clearfix""></div>"))
pnlTabs.Controls.Add(New LiteralControl("</div>"))
End If
End If
End Sub
#End Region
#Region "Bind Data"
Private FunctionBindData() As DataTable
Dim objSQL As New SqlDataProvider
Dim objBind As DataTable
'Caching
If Cache("Cache_VerticalTabs") Is Nothing Then
objBind = objSQL.FillTable("Pro_DynamicTabs_List", New ObjectPara("@IsVisible", 1))
Cache("Cache_VerticalTabs") = objBind
Else
objBind = CType(Cache("Cache_VerticalTabs"), DataTable)
End If
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
CreateTabs()
End If
Catch ex As Exception
End Try
End Sub
#End Region
End Class
End Namespace
Bây giờ chạy Project bạn sẽ có kết quả như ảnh phía dưới.
Chúc các bạn thành công!
Quang Bình
0 comments Blogger 0 Facebook
Post a Comment