Introducing ASP.NET Web Matrix
Pages: 1, 2, 3, 4, 5
Let's now code the application logic for our application:
Step 1.
We first add two global variables:
Dim ds as dataset
Dim productsInfo as ProductInfo
Step 2.
We will define a subroutine named getTitles() to
retrieve all of the titles from Amazon's Web service. First, access
the Web service:
Dim ws as new AmazonSearchService()
Dim KeywordReq as New KeywordRequest()
keywordReq.keyword = txtTitle.text
keywordReq.type="heavy"
keywordReq.devtag="D1M4DYSPL6TN9A"
keywordReq.version="1.0"
keywordReq.mode="books"
keywordReq.tag="mytag"
keywordReq.page= 1
productsInfo = ws.KeywordSearchRequest(keywordReq)
Step 3.
With the results stored in the productsInfo
object, you will now
convert the result into a Dataset object so that it can be bound
to the DataGrid control:
ds = new dataset()
ds.Tables.Add(New DataTable("products"))
ds.Tables("products").Columns.Add("No")
ds.Tables("products").Columns.Add("Title")
ds.Tables("products").Columns.Add("Publisher")
ds.Tables("products").Columns.Add("ISBN")
ds.Tables("products").Columns.Add("List Price")
ds.Tables("products").Columns.Add("Our Price")
Dim i as Integer
for i=0 to productsInfo.Details.Length - 1
Dim row As DataRow = _
ds.Tables("products").NewRow
row(0) = i + 1 ' to display #
row(1) = productsInfo.Details(i).ProductName
row(2) = productsInfo.Details(i).Manufacturer
row(3) = productsInfo.Details(i).ASIN
row(4) = productsInfo.Details(i).ListPrice
row(5) = productsInfo.Details(i).OurPrice
ds.Tables("products").Rows.Add(row)
Next i
Step 4.
Once the dataset is created, data bind it to the DataGrid control:
DataGrid1.DataSource = ds
DataGrid1.DataBind()
Step 5.
We also need to define an event
(SelectedIndexChanged) for the DataGrid control.
This event is fired when the user clicks on the Show Details link.
Sub DataGrid1_SelectedIndexChanged(sender As Object, _
e As EventArgs)
A call is first made to get the titles again:
getTitles()
The title is then displayed on the page:
dim curr as Details
curr = productsInfo.Details( _
datagrid1.datakeys(dataGrid1.SelectedIndex)-1)
response.write ("<b>" & curr.ProductName & _
"</b><br>by: ")
Followed by the display of the authors:
dim i as integer
for i = 0 to curr.authors.length-1
response.write (curr.authors(i).tostring)
if i < curr.authors.length-1 then
' not the last author
response.write (", ")
else
response.write ("<hr>")
end if
next i
And then, finally, write an <img> element to display the book cover image:
response.write ("<img src=" & curr.imageURLMedium & _
">")
End Sub
Step 6.
There are three more subroutines you need to modify:
Sub Page_Load(Sender As Object, E As EventArgs)
'----do nothing----
End Sub
Sub DataGrid_Page(Sender As Object,
e As DataGridPageChangedEventArgs)
'----when the user clicks on the next page----
DataGrid1.CurrentPageIndex = e.NewPageIndex
getTitles()
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
'----when the user clicks on the Search button----
getTitles()
End Sub
That's it! When you press F5 to run your application for the first time, the accompanying Web server will prompt you:
|
|


