Datalist 控件

结构跟Repeater很相似 多出一组EditItemTemplate项

在一下例子中使用CommandName属性指向某一个事件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DataList ID="DataList1" runat="server" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnEditCommand="DataList1_EditCommand" OnDeleteCommand="DataList1_DeleteCommand">
            <HeaderTemplate>
                <table style="width:900px;background-color:#ffffcc"><tr>
                    <td width="25%">姓名</td>
                    <td>性别</td>
                    <td>电话</td>
                    <td>操作</td>
                </tr></table>
            </HeaderTemplate>

            <ItemTemplate>
                <table style="width:900px;background-color:#ffffcc"><tr>
                    <td width="25%">
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("code") %>'></asp:Label></td>
                    <td width="25%"><%#Eval("name") %></td>
                    <td width="25%"><%#Eval("tel") %></td>
                    <td width="25%">
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">编辑</asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete">删除</asp:LinkButton>
                        
                    </td>
                </tr></table>
            </ItemTemplate>

            <EditItemTemplate>
                <table style="width:900px;background-color:#ffffcc"><tr>
                    <td width="25%">
                        <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td>
                    <td width="25%">
                        <asp:TextBox ID="txtsex" runat="server" Text='<%#Eval("sex") %>'></asp:TextBox></td>
                    <td width="25%">
                        <asp:TextBox ID="txttel" runat="server" Text='<%#Eval("tel") %>'></asp:TextBox></td>
                    <td width="25%"><asp:LinkButton ID="LinkButton1" runat="server">更新</asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server">取消</asp:LinkButton></td>
                </tr></table>
            </EditItemTemplate>
            <FooterTemplate></FooterTemplate>
        </asp:DataList>
    
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataList1.DataSource = new TelBF().Select();
            DataList1.DataBind();
        }
    }


    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        //选中改行的索引
        DataList1.EditItemIndex = e.Item.ItemIndex;//EditItemTemplate项默认索引为-1时隐藏该项
        //重新绑定数据
        DataList1.DataSource = new TelBF().Select();
        DataList1.DataBind();
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        Label la = (Label)e.Item.FindControl("Label1");
        string s = la.Text;
        new TelBF().Delete(s);
        DataList1.DataSource = new TelBF().Select();
        DataList1.DataBind();
    }
}

 

posted @ 2015-08-04 09:45  王文姿  阅读(421)  评论(0编辑  收藏  举报