Stock Query

To demonstrate additional techniques of data binding we present the StockStock.aspx page for showing the read-only query of items in stock. We use the Stock component from the previous chapter. This component implements the IStock interface and works against the Stock table in our database. We reference the relevant assemblies in the Stock.aspx file:


<%@ Assembly Name="StockItem" %>
<%@ Assembly Name="IStock" %>
<%@ Assembly Name="Stock" %>

Please refer to the readme.txt file in the Stock sample folder for the instructions on building the assemblies. Figure 16-15 shows the results of running the stock query.

Figure 16-15. Stock query.


To run this sample you should create a Web application for the Stock folder.

We modified the GetItems method of the Stock component to return a DataView object instead of ArrayList. We bind this object to the Repeater server control. This control allows defining the HTML template for rendering the items in the bound collection. We enclose this template by the <ItemTemplate> tags inside the Repeater control. Additionally, you may define the template for header and footer placing them in the <HeaderTemplate> and <FooterTemplate> tags respectively.

<asp:repeater runat="server" id="rptItems">
<HeaderTemplate>
<table align="center" width="95%" style="font: 
       8pt verdana">
<tr style="background-color:DFA894">
    <th>
    ID
    </th>
    <th>
    Name
    </th>
    <th>
    Price
    </th>
    <th>
    Quantity
    </th>
</tr>

</HeaderTemplate>
						<ItemTemplate>
<tr style="background-color:FFCCAA">
    <td align="center">
    <%# PerlNET::call("System.Web.UI.DataBinder.Eval",
						$Container, "DataItem.ID", "{0}") %>
    </td>
    <td align="center">
    <%# PerlNET::call("System.Web.UI.DataBinder.Eval",
						$Container, "DataItem.Name", "{0}") %>
    </td>
    <td align="center">
    <%# PerlNET::call("System.Web.UI.DataBinder.Eval",
						$Container, "DataItem.Price", "{0}") %>
    </td>
    <td align="center">
    <%# PerlNET::call("System.Web.UI.DataBinder.Eval", 
                 $Container, "DataItem.Quantity", "{0}") %>
    </td>
</tr>
</ItemTemplate>
						<FooterTemplate>
</table>

</FooterTemplate>

</asp:repeater>

To specify the data item to display we use the special binding syntax inside the <ItemTemplate> tags. The following code shows the ID of an item from the Stock table:

<%# PerlNET::call("System.Web.UI.DataBinder.Eval", 
                 $Container, "DataItem.ID", "{0}") %>

Notice that we enclose the binding code by the <%# . . . %> tags. We call the DataBinder.Eval method to evaluate the data item inside the bound collection of $Container. The $Container holds the reference to the Repeater control object. The last argument is optional and we may use it for formatting the output through the placeholders like in the Console.WriteLine method.

We bind the Repeater control to the data source in the BindControls method, which we invoke in the OnLoad event handler in the code-behind file.

#
# Stock.aspx.pm
#

package StockPage;

use namespace "System";
use namespace "System.Collections";
use namespace "System.Web";
use namespace "System.Web.UI";
use namespace "System.Web.UI.WebControls";
use namespace "System.Data";
use namespace "OI.Samples";

use PerlNET qw(AUTOCALL int decimal in);

=for interface
    [extends: System.Web.UI.Page]
    protected override void OnLoad(EventArgs e);
    protected void BindControls();
						protected field Repeater rptItems;
=cut

sub OnLoad
{
    my $this = shift;

    $this->BindControls();
}

sub BindControls
{
    my $this = shift;

    my $stock = "Stock"->new();

    my $items = $stock->GetItems();
						$this->{rptItems}->{DataSource} = $items;
						$this->{rptItems}->DataBind();
}

You may run the Stock Management System application from the previous chapter and examine how the results of the stock query reflect the changes you make through the Windows Forms application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.168.16