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;
using System.Text; // needed for StringBuilder
using
System.IO; // needed for StringWriter
namespace GridViews
{
public partial class DynamicDisplay2 : 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();
}
}
protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
GenerateProductsTable(ddlProducts.SelectedValue);
}
// This procedure builds an HTML 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
String strHTML = "";
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];
if (dtProducts.Rows.Count != 0)
{
//
Start the HTML table
strHTML = strHTML + "<table>" +
"<tr style='font-weight:bold'>" +
"<td> Product ID </td>" +
"<td> Description </td>" +
"<td> Quantity in
Stock </td>" +
"<td> Price </td>" +
"</tr>";
//
Build an HTML table row for each record in the DataTable
dtProducts
//
with data contained in the record.
for (int
row = 0; row < dtProducts.Rows.Count; row++)
{
// Retrieve a reference to the current DataRow
(record in the DataTable
drProductRecord = dtProducts.Rows[row];
// Create a Texbox
used to bind the QOH for each record
TextBox TBox = new TextBox();
String TBoxHTML
= "";
TBox.Text
= drProductRecord["QOH"].ToString();
TBox.ID = "txt" + row;
TBox.Width
= 50;
// Render a server control object into an HTML string
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
TBox.RenderControl(htmlWriter);
TBoxHTML
= strBuilder.ToString();
// Create an HTML row for this
record and its values
strHTML = strHTML + "<tr>" +
"<td>" + drProductRecord["ProductNumber"] + "</td>" +
"<td>" + drProductRecord["Description"] + "</td>" +
"<td>" + TBoxHTML + "</td>" +
"<td>" + String.Format("{0:c}", drProductRecord["Price"]) + "</td>" +
"</tr>";
}
// End
the HTML table
strHTML += "</table>";
}
// Write
the HTML string for the table to a DIV in the HTML markup of the ASPX page
divDisplay.InnerHtml
= strHTML;
}
}
}