Datalist 控件

結構跟Repeater很類似 多出一組EditItemTemplate項html

在一下例子中使用CommandName屬性指向某一個事件ui

<%@ 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();
    }
}
相關文章
相關標籤/搜索