This sample demonstrates a product-listing user control that internally handles its own postback events. A user control can hand
CS\Pagelet6.ascx
<%@ 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("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
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("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
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 style="font: 10.5pt 宋体">
<tr>
<td><b>选择类别:</b></td>
<td style="padding-left:15">
<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>
<ASP:DataList id="MyDataList" BorderWidth="0" RepeatColumns="2" runat="server">
<ItemTemplate>
<table cellpadding=10 style="font: 10.5pt 宋体">
<tr>
<td valign="top">
<img align="top" src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' >
</td>
<td valign="top">
<b>书名:</b><%# DataBinder.Eval(Container.DataItem, "title") %><br>
<b>类别:</b><%# DataBinder.Eval(Container.DataItem, "type") %><br>
<b>出版商 ID:</b><%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>价格:</b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
</td>
</tr>
</table>
</ItemTemplate>
</ASP:DataList>
CS\Pagelet6.aspx
<%@ Page Language="C#" %>
<%@ Register TagPrefix="Acme" TagName="BookList" Src="pagelet6.ascx" %>
<html>
<body style="font: 10.5pt 宋体">
<h3>具有事件的用户控件</h3>
<form runat="server">
<Acme:BookList runat="server"/>
</form>
</body>
</html>