028. asp.net数据绑定控件值DataList控件

DataList控件可以使用模板与定义样式来显示数据并进行数据的选择, 删除及编辑工作. DataList控件的最大特点是一定要通过模板来定义数据的显示格式. 如果要设计出美观的界面, 就需要花费一番心思. DataList控件显示数据时具有灵活性, 开发人员发挥的空间较大, DataList支持的模板如下:

AlternationItemTemplate

         如果已经定义, 则为DataList中的交替项提供内容和布局; 如果未定义, 则使用ItemTemplate

EditItemTemplate

         如果已经定义, 则为DataList中的当前编辑项提供内容和布局; 如果未定义则使用ItemTemplate

FooterTemplate

         如果已经定义, 则为DataList的脚注部分提供内容和布局; 如果未定义则不显示脚注部分

HeaderTemplate

         如果已经定义, 则为DataList的页眉部分提供内容和布局; 如果未定义则不显示页眉部分

ItemTemplate

         为DataList中的项提供内容和布局所要求的模板

SelectdItemTemplate

         如果已经定义, 则为DataList中的当前选定项提供内容和布局; 如果未定义则使用ItemTemplate

SeparatorTemplate

         如果已经定义, 则为DataList中的分隔符提供内容和布局; 如果未定义则不显示分隔符

 

下面是关于dataList控件的前端代码简单示例:

<asp:DataList ID="DataList1" runat="server" Width="239px">
                        <FooterTemplate>
                            <table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
                                        下面这行是合计</td>
                                </tr>
                                <tr>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        编号合计</td>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        姓名合计</td>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        性别合计</td>
                                    <td style="width: 150px; height: 19px; color: #669900;">
                                        内号码合计</td>
                                </tr>
                            </table>
                        </FooterTemplate>
                        <HeaderTemplate>
                            <table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
                                        使用DataList控件绑定数据源</td>
                                </tr>
                                <tr>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        编号</td>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        姓名</td>
                                    <td style="height: 19px; width: 50px; color: #669900;">
                                        性别</td>
                                    <td style="width: 150px; height: 19px; color: #669900;">
                                        内号码</td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table border="1" style="width: 300px; color: #000000; text-align: center;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="height: 21px; width: 50px; color: #669900;">
                                        <asp:Label ID="lblStuID" runat="server" Text='<%# Eval("cardNo") %>'></asp:Label></td>
                                    <td style="height: 21px; width: 50px; color: #669900;">
                                        <asp:Label ID="lblStuName" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
                                    <td style="height: 21px; width: 50px; color: #669900;">
                                        <asp:Label ID="lblStuSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label></td>
                                    <td style="width: 150px; height: 21px; color: #669900;">
                                        <asp:Label ID="lblstuHobby" runat="server" Text='<%# Eval("cardBound") %>'></asp:Label></td>

                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>

对应的后台代码:

 1 protected void Page_Load(object sender, EventArgs e)
 2 
 3     {
 4 
 5         if (!IsPostBack)
 6 
 7         {
 8 
 9             //实例化SqlConnection对象
10 
11             SqlConnection sqlCon = new SqlConnection();
12 
13             //实例化SqlConnection对象连接数据库的字符串
14 
15             sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";
16 
17             //定义SQL语句
18 
19             string SqlStr = "select * from card";
20 
21             //实例化SqlDataAdapter对象
22 
23             SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
24 
25             //实例化数据集DataSet
26 
27             DataSet ds = new DataSet();
28 
29             da.Fill(ds, "card");
30 
31             //绑定DataList控件
32 
33             DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
34 
35             DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源
36 
37         }
38 
39     }

 

对应的显示效果图:

 

DataList一些基本的事件使用简单示例:

  1 public SqlConnection GetCon()
  2 
  3     {
  4 
  5         //实例化SqlConnection对象
  6 
  7         SqlConnection sqlCon = new SqlConnection();
  8 
  9         //实例化SqlConnection对象连接数据库的字符串
 10 
 11         sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";
 12 
 13         return sqlCon;
 14 
 15     }
 16 
 17     public void Bind()
 18 
 19     {
 20 
 21         SqlConnection sqlCon = GetCon();
 22 
 23         //定义SQL语句
 24 
 25         string SqlStr = "select * from card";
 26 
 27         //实例化SqlDataAdapter对象
 28 
 29         SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
 30 
 31         //实例化数据集DataSet
 32 
 33         DataSet ds = new DataSet();
 34 
 35         da.Fill(ds, "card");
 36 
 37         //绑定DataList控件
 38 
 39         DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
 40 
 41         DataList1.DataKeyField = "cardNo";//设置数据表的主键
 42 
 43         DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源
 44 
 45     }
 46 
 47  
 48 
 49     protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
 50 
 51     {
 52 
 53         //设置DataList1控件的编辑项的索引为选择的当前索引
 54 
 55         DataList1.EditItemIndex = e.Item.ItemIndex;
 56 
 57         //数据绑定
 58 
 59         Bind();
 60 
 61     }
 62 
 63     protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
 64 
 65     {
 66 
 67         //设置DataList1控件的编辑项的索引为-1,即取消编辑
 68 
 69         DataList1.EditItemIndex = -1;
 70 
 71         //数据绑定
 72 
 73         Bind();
 74 
 75  
 76 
 77     }
 78 
 79     protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
 80 
 81     {
 82 
 83         //取得编辑行的关键字段的值
 84 
 85         string stuID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
 86 
 87         //取得文本框中输入的内容
 88 
 89         string stuName = ((TextBox)e.Item.FindControl("txtName")).Text;
 90 
 91         string stuSex = ((TextBox)e.Item.FindControl("txtSex")).Text;
 92 
 93         string stuHobby = ((TextBox)e.Item.FindControl("txtHobby")).Text;
 94 
 95         string sqlStr = "update card set name='" + stuName + "',sex='" + stuSex + "',cardBound='" + stuHobby + "' where cardNo=" + stuID;
 96 
 97         //更新数据库
 98 
 99         SqlConnection myConn = GetCon();
100 
101         myConn.Open();
102 
103         SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
104 
105         myCmd.ExecuteNonQuery();
106 
107         myCmd.Dispose();
108 
109         myConn.Close();
110 
111         //取消编辑状态
112 
113         DataList1.EditItemIndex = -1;
114 
115         Bind();
116 
117     }

 

posted on 2016-12-14 09:56  印子  阅读(4642)  评论(0编辑  收藏  举报

导航