zoukankan      html  css  js  c++  java
  • DataList 编辑、删除、更新、取消

    前台页面:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication2.WebForm4" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
     
    <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

     
  • 相关阅读:
    Joint Consensus两阶段成员变更的单步实现
    深度干货|云原生分布式数据库 PolarDBX 的技术演进
    SpringMVC框架入门配置 IDEA下搭建Maven项目
    windows安装composer方法和使用方法
    idea2016 spring 新手上路
    jQuery 获取 attr() 与 prop() 属性值的方法及区别介绍 _fei
    处女座的看过来【 JetBrains强迫症】注释篇
    phpstorm 配置 xdebug调试工具
    使用Intellij IDEA整合Spring+Spring MVC+MyBitis
    长链剖分小记
  • 原文地址:https://www.cnblogs.com/jiangyongyawen/p/4241360.html
Copyright © 2011-2022 走看看