今天天气不错,有点秋天的味儿了.我喜欢秋天.
但是头有点晕晕的.
学习了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>