2004/08/28-Web 窗体用户控件例子-具有事件的用户控件

今天天气不错,有点秋天的味儿了.我喜欢秋天.
但是头有点晕晕的.

学习了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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值