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>
对应的后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //实例化SqlConnection对象 SqlConnection sqlCon = new SqlConnection(); //实例化SqlConnection对象连接数据库的字符串 sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW"; //定义SQL语句 string SqlStr = "select * from card"; //实例化SqlDataAdapter对象 SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon); //实例化数据集DataSet DataSet ds = new DataSet(); da.Fill(ds, "card"); //绑定DataList控件 DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表 DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源 } }
对应的显示效果图:
DataList一些基本的事件使用简单示例:
public SqlConnection GetCon() { //实例化SqlConnection对象 SqlConnection sqlCon = new SqlConnection(); //实例化SqlConnection对象连接数据库的字符串 sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW"; return sqlCon; } public void Bind() { SqlConnection sqlCon = GetCon(); //定义SQL语句 string SqlStr = "select * from card"; //实例化SqlDataAdapter对象 SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon); //实例化数据集DataSet DataSet ds = new DataSet(); da.Fill(ds, "card"); //绑定DataList控件 DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表 DataList1.DataKeyField = "cardNo";//设置数据表的主键 DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源 } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { //设置DataList1控件的编辑项的索引为选择的当前索引 DataList1.EditItemIndex = e.Item.ItemIndex; //数据绑定 Bind(); } protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { //设置DataList1控件的编辑项的索引为-1,即取消编辑 DataList1.EditItemIndex = -; //数据绑定 Bind(); } protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { //取得编辑行的关键字段的值 string stuID = DataList1.DataKeys[e.Item.ItemIndex].ToString(); //取得文本框中输入的内容 string stuName = ((TextBox)e.Item.FindControl("txtName")).Text; string stuSex = ((TextBox)e.Item.FindControl("txtSex")).Text; string stuHobby = ((TextBox)e.Item.FindControl("txtHobby")).Text; string sqlStr = "update card set name='" + stuName + "',sex='" + stuSex + "',cardBound='" + stuHobby + "' where cardNo=" + stuID; //更新数据库 SqlConnection myConn = GetCon(); myConn.Open(); SqlCommand myCmd = new SqlCommand(sqlStr, myConn); myCmd.ExecuteNonQuery(); myCmd.Dispose(); myConn.Close(); //取消编辑状态 DataList1.EditItemIndex = -; Bind(); }