Introduction to Asp+ Syntax (6)----Database Chapter
Author:Eve Cole
Update Time:2009-05-30 19:54:02
ASP+ provides us with a set of data table and data list controls. These controls can help us customize our UI (user interFace user interface) without considering one database or another. For example: In the following example, we will introduce how the <asp:datagrid runat=server> control passes
The sql statement provides us with data
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
<head>
<link rel="stylesheet"href="intro.css">
</head>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataSetCommand
'The following is the database connection
MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.Value + "'", myConnection)
DS = new DataSet()
MyCommand.FillDataSet(DS, "Titles")
MyList.DataSource = DS.Tables("Titles").DefaultView
MyList.DataBind()
End Sub
</script>
<body>
<center>
<form action="intro75.aspx" method="post" runat="server">
<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
<asp:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<ASP:DataGrid id="MyList" HeaderStyle-BackColor="#aaaadd" BackColor="#ccccff" runat="server"/>
</form>
</center>
</body>
</html>
A running example of this example is at
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro75.aspx
Data grid control <asp:datagrid runat=server>
It provides us with a very simple method to use traditional UI sections to display the results of data queries. Asp+ developers can now also use <asp:dataList
runat=server>
To customize the data list display to customize the information
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
<head>
<link rel="stylesheet"href="intro.css">
</head>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataSetCommand
MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.Value + "'", myConnection)
DS = new DataSet()
MyCommand.FillDataSet(DS, "Titles")
MyList.DataSource = DS.Tables("Titles").DefaultView
MyList.DataBind()
End Sub
</script>
<body>
<center>
<form action="intro8.aspx" method="post" runat="server">
<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
<asp:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<asp:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">
<template name="itemtemplate">
<table>
<tr>
<td>
<img src='<%# DataBinder.Eval
(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>'>
</td>
<td width=250 valign=top>
<b><%# DataBinder.Eval(Container.DataItem, "title") %></b>
<br><br>
Price: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %>
</td>
</tr>
</table>
</template>
</asp:datalist>
</form>
</center>
</body>
</html>
An example of running this program is in
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro8.aspx