Wednesday, April 2, 2008

ASP.NET AJAX Tabs Control





The Tabs control allows you to create the Tabs very quickly. The tabs can be changed without causing a postback. This tabs based on the data from the Categories and Products table in the Northwind database(i this example i have used dummy data simmilar to northwind database)



<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="DynamicTabPanelGridViewGuy.aspx.cs"
Inherits="DynamicTabPanelGridViewGuy" %>


<%@ Register Assembly="AjaxControlToolkit" TagPrefix="ajaxToolkit"
Namespace="AjaxControlToolkit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server"
ActiveTabIndex="0">
</ajaxToolkit:TabContainer>
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 AjaxControlToolkit;
using System.Collections.Generic;

public partial class DynamicTabPanelGridViewGuy : System.Web.UI.Page
{
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.BackColor = System.Drawing.Color.Wheat;

gv.DataSource = category.Products;

gv.DataBind();

AjaxControlToolkit.TabPanel tab = new AjaxControlToolkit.TabPanel();

tab.Controls.Add(gv);

tab.HeaderText = category.CategoryName;

TabContainer1.Tabs.Add(tab);

}

}

}
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>();

for (int i = 1; i <= 5; i++)
{


Category category = new Category();

category.CategoryID = i;

category.CategoryName = "Column1Data" + i.ToString();

categories.Add(category);

}

// 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>();

if (categoryID == 1)
{
for (int i = 1; i <= 20; i++)
{

Product product = new Product();

product.ProductID = i;

product.ProductName = "Product1";

products.Add(product);

}

}
if (categoryID == 2)
{
for (int i = 1; i <= 20; i++)
{

Product product = new Product();

product.ProductID = i;

product.ProductName = "Product2";

products.Add(product);

}

}
if (categoryID == 5)
{
for (int i = 1; i <= 20; i++)
{

Product product = new Product();

product.ProductID = i;

product.ProductName = "product5";

products.Add(product);

}

}


return products;

}


}
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()
{

}

}
}

No comments:

Post a Comment