using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using
Utilities;
using System.Data;
namespace GridViews
{
public partial class DynamicDisplay3 : System.Web.UI.Page
{
DBConnect objDB = new DBConnect();
String strSQL = "";
protected void Page_Load(object sender, EventArgs e)
{
// Bind
the dataset data to the drop-down list during intial
page load
// set
the text displayed in the list to the DeparmentName
field value
// and
the value of a selected item to the DepartmentNumber
field value
if (!IsPostBack)
{
strSQL = "SELECT
* FROM Department";
ddlProducts.DataSource
= objDB.GetDataSet(strSQL);
ddlProducts.DataTextField
= "DepartmentName";
ddlProducts.DataValueField
= "DepartmentNumber";
ddlProducts.DataBind();
}
else
{
GenerateProductsTable(ddlProducts.SelectedValue); //
key to getting table redisplayed in postbacks and
button events to work.
}
}
protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
lblSelect.Text
= "";
GenerateProductsTable(ddlProducts.SelectedValue);
}
// This procedure builds a table that displays information
about each record
//
contained in the DataSet dsProductList
private void GenerateProductsTable(String theDepartmentID)
{
DataSet dsProductList;
DataTable dtProducts; // used to reference a table in the dataset
DataRow drProductRecord; // used to reference a singe row
(record) from the dataset
strSQL = "SELECT
ProductNumber, Description, QOH, Price FROM Product
" +
"WHERE
DepartmentNumber = " + theDepartmentID;
dsProductList = objDB.GetDataSet(strSQL);
//
Retrieve a reference to the table in the DataSet that
contains the records
//
returned by the SQL query.
dtProducts
= dsProductList.Tables[0];
MyPlaceHolder.Controls.Clear();
if (dtProducts.Rows.Count != 0)
{
//
Build a table and a row for each record in the DataTable
dtProducts
//
with data contained in the record.
Table tblRecords = new Table();
MyPlaceHolder.Controls.Add(tblRecords);
//ViewState["DynamicTable"] = true;
for (int
row = 0; row < dtProducts.Rows.Count; row++)
{
TableRow productRow = new TableRow();
TableCell productIDCell = new TableCell();
TableCell productDescCell = new TableCell();
TableCell productQtyCell = new TableCell();
TableCell productPriceCell = new TableCell();
TableCell textProductCell = new TableCell();
TableCell selectProductCell = new TableCell();
// Retrieve a reference to the current DataRow
(record in the DataTable) and store the values for
each field
// in the appropriate table cell.
drProductRecord = dtProducts.Rows[row];
productIDCell.Text
= drProductRecord["ProductNumber"].ToString();
productDescCell.Text
= drProductRecord["Description"].ToString();
productQtyCell.Text
= drProductRecord["QOH"].ToString();
productPriceCell.Text
= drProductRecord["Price"].ToString();
// Create a Button used select an
item
Button objButton
= new Button();
objButton.Text
= "Select Product";
objButton.ID = "btn" + row;
objButton.Width
= 120;
objButton.Click
+= new EventHandler(this.SelectButtonHandler);
selectProductCell.Controls.Add(objButton);
TextBox objTBox = new TextBox();
objTBox.ID = "txt" + row;
objTBox.Width
= 50;
textProductCell.Controls.Add(objTBox);
// Add the dynamically generated
table cells to a row and add the row to the table
productRow.Cells.Add(productIDCell);
productRow.Cells.Add(productDescCell);
productRow.Cells.Add(productQtyCell);
productRow.Cells.Add(productPriceCell);
productRow.Cells.Add(textProductCell);
productRow.Cells.Add(selectProductCell);
//example works without the following code
//productRow.EnableViewState =
true;
//productIDCell.EnableViewState
= true;
//productDescCell.EnableViewState
= true;
//productQtyCell.EnableViewState
= true;
//productPriceCell.EnableViewState
= true;
tblRecords.Rows.Add(productRow);
}
}
//this.Rows = dtProducts.Rows.Count;
//this.Columns = 5;
}
public void SelectButtonHandler(Object sender, EventArgs e)
{
Button button = (Button)sender;
lblSelect.Text
= "You selected product
" + button.ID;
}
}
}