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;

public partial class DataGrid_Templates_Categories : System.Web.UI.UserControl
{

    //Declarations
    public APNSoft.WebControls.GridRow GridRow = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        //Declarations
        DataTable dtCategories = null;

        //Get Categories (Cached in session for best performance)
        object SessCategories = Session["SessCategories"];
        if (SessCategories == null)
        {
            string SqlQuery = "SELECT * FROM Categories ORDER BY CategoryName";
            dtCategories = DataBase.GetDataTableOleDb(SqlQuery, "~/DataGrid/DataBases/Nwind.mdb");
            Session["SessCategories"] = dtCategories;
        }
        else
        {
            dtCategories = (DataTable)Session["SessCategories"];
        }


        //Add JavaScript call
        ddlCategories.Attributes.Add("onchange", "UpdateDB(this);");

        //Add Row ID
        ddlCategories.Attributes.Add("RowID", GridRow.RowID);

        //Add Categories
        for (int i = 0; i < dtCategories.Rows.Count; i++)
        {
            //Get CategoryID & CategoryName
            int CategoryID = (Int32)dtCategories.Rows[i]["CategoryID"];
            string CategoryName = (string)dtCategories.Rows[i]["CategoryName"];

            //Create ListItem
            ListItem Category = new ListItem(CategoryName, CategoryID.ToString());
            if ((Int32)GridRow.Cells["CategoryID"].Value == CategoryID)
            {
                Category.Selected = true;
            }

            //Append Category
            ddlCategories.Items.Add(Category);
        }
    }

}