• 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="900px;background-color:#ffffcc"><tr>
                        <td width="25%">姓名</td>
                        <td>性别</td>
                        <td>电话</td>
                        <td>操作</td>
                    </tr></table>
                </HeaderTemplate>
    
                <ItemTemplate>
                    <table style="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="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();
        }
    }
  • 相关阅读:
    Dubbo2.0
    Dubbo--RPC框架
    ActiveMQ消息队列
    quartz开源任务调度框架
    webService
    crud最常用
    springBoot第三天
    springmvc--依赖
    springBoot第二天
    pom.xml和settings.xml的下载优先级
  • 原文地址:https://www.cnblogs.com/happinesshappy/p/4698513.html
Copyright © 2020-2023  润新知