今天天氣不錯,有點秋天的味兒了.我喜歡秋天.
但是頭有點暈暈的.
學習了Web 窗體用戶控件,可重用性很強.感覺有點向ASP中的Include.寫了一個具有事件的控件.
頁面代碼如下:BookList.aspx
<%@ Page Language="C#" Debug="True" %>
<%@ Register TagPrefix="Acme" TagName="BookList" Src="BookList.ascx" %>
<html>
<head>
</head>
<body>
<center>
<form runat="server">
<p style="FONT-SIZE: large">
具有事件的用戶控件
</p>
<p>
<Acme:BookList id="UserControl1" runat="server"></Acme:BookList>
</p>
</form>
</center>
</body>
</html>
邏輯代碼如下:BookList.ascx
<%@ Control Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script language="c#" runat="server">
void Page_Load(Object Src, EventArgs e) {
if (!Page.IsPostBack) {
SqlConnection myConnection = new SqlConnection("Data Source=localhost;UID=sa;PWD=123456;Initial Catalog=pubs");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from titles where type='" + Category.SelectedItem.Value + "'",myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds,"書名");
MyDataList.DataSource = ds.Tables["書名"].DefaultView;
MyDataList.DataBind();
}
}
void Category_Select(Object sender, EventArgs E) {
SqlConnection myConnection = new SqlConnection("Data Source=localhost;UID=sa;PWD=123456;Initial Catalog=pubs");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from titles where type='" + Category.SelectedItem.Value + "'",myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds,"書名");
MyDataList.DataSource = ds.Tables["書名"].DefaultView;
MyDataList.DataBind();
}
</script>
<table width="224" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>選擇類別:</td>
<td>
<asp:DropDownList AutoPostBack="true" ID="Category" OnSelectedIndexChanged="Category_Select" runat="server">
<asp:ListItem value="business">商業</asp:ListItem>
<asp:ListItem value="trad_cook">傳統烹飪</asp:ListItem>
<asp:ListItem value="mod_cook">現代烹飪</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<br>
<asp:DataList ID="MyDataList" RepeatColumns="2" runat="server">
<ItemTemplate>
<table>
<tr>
<td valign="top">
<img align="top" src="fang.gif">
</td>
<td valign="top">
書名:<%# DataBinder.Eval(Container.DataItem, "title") %><br>
類別:<%# DataBinder.Eval(Container.DataItem, "type") %><br>
出版商:<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
價格:<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>