首页 > 技术文章 > DataList 编辑、删除、更新、取消

jiangyongyawen 2015-01-22 12:37 原文

前台页面:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication2.WebForm4" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" 
            OnCancelcommand="DataList1_CancelCommand" 
            OnUpdatecommand="DataList1_UpdateCommand" 
            oneditcommand="DataList1_EditCommand" >
            <HeaderTemplate>
            <asp:Label ID="lbl22" runat="Server" Font-Size="20" Text="编号"></asp:Label>
            <asp:Label ID="Label2" runat="Server" Font-Size="20"  Text="姓名"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" 
    Text='<%# Eval("no") %>'></asp:Label>
            <asp:Label ID="lbl" runat="Server" Text='<%# Eval("name") %>'></asp:Label>
            <asp:LinkButton ID="lk1" runat="Server" CommandArgument='<%# Eval("id") %>'  CommandName="edit">编辑</asp:LinkButton>
            <asp:LinkButton ID="LinkButton1" runat="Server"  CommandArgument='<%# Eval("id") %>'  CommandName="delete">删除</asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox  ID="txt_no"  runat="Server" Text='<%# Eval("no") %>'></asp:TextBox>
            <asp:TextBox  ID="txt_name"  runat="Server" Text='<%# Eval("name") %>'></asp:TextBox>
             <asp:LinkButton ID="lk1" runat="Server"    CommandArgument='<%# Eval("id") %>' CommandName="update">更新</asp:LinkButton>
            <asp:LinkButton ID="LinkButton1" runat="Server"  CommandArgument='<%# Eval("id") %>' CommandName="cancel">取消</asp:LinkButton>
 
            </EditItemTemplate>
        </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;
using System.Data;
using System.Data.SqlClient;
 
namespace WebApplication2
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { Bind(); }
        }
        private void Bind()
        {
            this.DataList1.DataSource = getTable();
            this.DataList1.DataBind();
        }
        private DataTable getTable()
        {
            SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=fiybird");
            SqlDataAdapter apt = new SqlDataAdapter("select * from stu",con);
            DataTable dt = new DataTable();
            apt.Fill(dt);
            return dt;
        }
 
        protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            string no = (e.Item.FindControl("txt_no") as TextBox).Text;
            string name = (e.Item.FindControl("txt_name") as TextBox).Text;
            if (update(no, name, id))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(),"","alert('修改成功!');",true);
                this.DataList1.EditItemIndex=-1;
                Bind();
            }
        }
        private bool update(string no, string name, int id)
        {
            string sql = string.Format("update stu set no='{0}',name='{1}' where id={2}",no,name,id);
            SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=fiybird");
            try
            {
                SqlCommand cmd = new SqlCommand(sql,con);
                con.Open();
                return cmd.ExecuteNonQuery() > 0;
            }
            catch (System.Exception ex)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
 
        protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            this.DataList1.EditItemIndex = e.Item.ItemIndex;
            Bind();
        }
 
        protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.DataList1.EditItemIndex = -1;
            Bind();
        }
 
       
 
    }
} 那么删除要怎么处理呢?  有UpdateCommand、EditCommand、CancelCommand 是否有DeleteCommand事件呢?  去动手试试看。。。

若有疑问可以联系我www.jiangyong.net.cn,里面有我的QQ

 

推荐阅读