ASP.NET AJAX Tabs Control
Sunday, February 24, 2008 10:19:46 AM
The Tabs control allows you to create the Tabs very quickly. The tabs can be changed without causing a postback. I recently created tabs based on the data from the Categories and Products table in the Northwind database.
Check out the code below which creates the TabPanel dynamically and then add to the TabsContainer control.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Category> categories = Category.GetCategories();
foreach (Category category in categories)
{
GridView gv = new GridView();
gv.DataSource = category.Products;
gv.DataBind();
AjaxControlToolkit.TabPanel tab = new AjaxControlToolkit.TabPanel();
tab.Controls.Add(gv);
tab.HeaderText = category.CategoryName;
TabContainer1.Tabs.Add(tab);
}
}
}
You can use the following code for initialial connect to database and builds methods to retrive data from database.
Here is the List Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
public class Category
{
private int categoryID;
private string categoryName;
private List<Product> products;
public int CategoryID
{
get { return this.categoryID; }
set { this.categoryID = value; }
}
public string CategoryName
{
get { return this.categoryName; }
set { this.categoryName = value; }
}
public List<Product> Products
{
get { return this.products; }
set { this.products = value; }
}
public static List<Category> GetCategories()
{
List<Category> categories = new List<Category>();
string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", myConnection);
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
Category category = new Category();
category.CategoryID = (int)reader["CategoryID"];
category.CategoryName = (string)reader["CategoryName"];
categories.Add(category);
}
myConnection.Close();
reader.Close();
// get the products
foreach (Category category in categories)
{
category.Products = GetProducts(category.CategoryID);
}
return categories;
}
public static List<Product> GetProducts(int categoryID)
{
List<Product> products = new List<Product>();
string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT ProductID, ProductName FROM Products WHERE CategoryID = " + categoryID,myConnection);
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
Product product = new Product();
product.ProductID = (int)reader["ProductID"];
product.ProductName = (string)reader["ProductName"];
products.Add(product);
}
myConnection.Close();
reader.Close();
return products;
}
public Category()
{
}
}
ANd Product.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class Product
{
private int productID;
private string productName;
public int ProductID
{
get { return this.productID; }
set { this.productID = value; }
}
public string ProductName
{
get { return this.productName; }
set { this.productName = value; }
}
public Product()
{
}
}
Hope this helps.







Unregistered user # Thursday, May 1, 2008 9:20:16 AM