datalist用法整合

前台
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:DataList ID="DataList1" runat="server" Width="100%" DataKeyField="ID"
        oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand"
        onitemcommand="DataList1_ItemCommand" onupdatecommand="DataList1_UpdateCommand">
    <HeaderTemplate><table width="80%" ><tr><td>选择</td><td>头像</td><td>学号</td><td>姓名</td><td>地址</td><td>编辑</td><td>操作</td></tr></HeaderTemplate>
    <ItemTemplate><tr>
    <td><asp:CheckBox ID="select" runat="server" Text="选择" /></td>
    <td><asp:Image ID="image" runat="server" Width="50" Height="60" ImageUrl="~/image/5.jpg" /></td>
    <td><asp:Label ID="label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "StuID")%>'></asp:Label></td>
    <td><asp:Label ID="label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Stuname")%>'></asp:Label></td>
    <td><asp:Label ID="label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Stuadduml")%>'></asp:Label></td>
    <td><asp:Button ID="btn1" runat="server" Text="编辑" CommandName="edit" /></td>
    <td><asp:Button ID="btn2" runat="server" Text="接受" CommandName="accept" />  <asp:Button ID="btn3" runat="server" Text="拒绝" CommandName="noaccept" /></td></tr></ItemTemplate>
    <SeparatorTemplate><tr><td colspan="7"><hr style="background-color:Blue"/></td></tr></SeparatorTemplate>
    <EditItemTemplate><tr>
    <td><asp:Button ID="btnupdate" runat="server" Text="更新" CommandName="update" /></td>
    <td><asp:Button ID="btncancel" runat="server" Text="取消" CommandName="cancel" /></td>
    <td><asp:TextBox ID="txtstuid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "StuID")%>'></asp:TextBox></td>
    <td><asp:TextBox ID="txtstuname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Stuname")%>'></asp:TextBox></td>
    <td><asp:TextBox ID="txtstuadd" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Stuadduml")%>'></asp:TextBox></td></tr></EditItemTemplate>
    </asp:DataList>
    <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="全选" />
    <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="接受选定项" />
    <asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="全部拒绝" />
    <asp:Button ID="Button4" runat="server" οnclick="Button4_Click" Text="全部删除" />
    <br />
    <asp:Button ID="Button5" runat="server" οnclick="Button5_Click" Text="添加" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </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;

public partial class _Default : System.Web.UI.Page
{
    string strcon = "server=.;database=pubs;uid=sa;pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        SqlConnection cn = new SqlConnection(strcon);
        cn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from students", cn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        cn.Close();
        this.DataList1.DataSource = dt;
        this.DataList1.DataBind();
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        bind();
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = -1;
        bind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandSource.GetType() == typeof(Button))
        {
                if (((Button)e.CommandSource).CommandName == "accept")
                {
                    if (((CheckBox)e.Item.FindControl("select")).Checked)
                    {

                    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    int stuid = Convert.ToInt32(((Label)e.Item.FindControl("label1")).Text);
                    string stuname = ((Label)e.Item.FindControl("label2")).Text;
                    string stuadd = ((Label)e.Item.FindControl("label3")).Text;
                    SqlConnection cn = new SqlConnection(strcon);
                    SqlCommand cmd = new SqlCommand("insert into s(StuID,StuName,Stuaddurl) values('" + stuid + "','" + stuname + "','" + stuadd + "')", cn);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    SqlCommand cmd1 = new SqlCommand("delete from students where ID='" + id + "'", cn);
                    cmd1.ExecuteNonQuery();
                    cn.Close();
                    bind();
                    }
                }
                if (((Button)e.CommandSource).CommandName == "noaccept")
                {
                    if (((CheckBox)e.Item.FindControl("select")).Checked)
                    {
                    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    int stuid = Convert.ToInt32(((Label)e.Item.FindControl("label1")).Text);
                    string stuname = ((Label)e.Item.FindControl("label2")).Text;
                    string stuadd = ((Label)e.Item.FindControl("label3")).Text;
                    SqlConnection cn = new SqlConnection(strcon);
                    SqlCommand cmd = new SqlCommand("insert into sj(stuid,stuname,stuadd) values('" + stuid + "','" + stuname + "','" + stuadd + "')", cn);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    SqlCommand cmd1 = new SqlCommand("delete from students where ID='" + id + "'", cn);
                    cmd1.ExecuteNonQuery();
                    cn.Close();
                    bind();
                }
            }
        }
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        int stuid = Convert.ToInt32(((TextBox)e.Item.FindControl("txtstuid")).Text);
        string stuname = ((TextBox)e.Item.FindControl("txtstuname")).Text;
        string stuadd = ((TextBox)e.Item.FindControl("txtstuadd")).Text;
        SqlConnection cn = new SqlConnection(strcon);
        cn.Open();
        SqlCommand cmd = new SqlCommand("update students set StuID='" + stuid + "',Stuname='" + stuname + "',Stuadduml='" + stuadd + "'where ID='" + id + "'", cn);
        cmd.ExecuteNonQuery();
        cn.Close();
        Response.Write("<script>alert('ok')</script>");
        DataList1.EditItemIndex = -1;
        bind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1.Text = "全选";
        }
        if (Button1.Text == "全选")
        {
            foreach (DataListItem di in this.DataList1.Items)
            {
                CheckBox ck = (CheckBox)di.FindControl("select");
                ck.Checked = true;
            }
            this.Button1.Text = "全部取消";
        }
        else
        {
            foreach (DataListItem di in this.DataList1.Items)
            {
                CheckBox ck = (CheckBox)di.FindControl("select");
                ck.Checked = false;
            }
            Button1.Text = "全选";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            if (((CheckBox)DataList1.Items[i].FindControl("select")).Checked)
            {
                int id = Convert.ToInt32(DataList1.DataKeys[i].ToString());
                int stuid = Convert.ToInt32(((Label)DataList1.Items[i].FindControl("label1")).Text);
                string stuname = ((Label)DataList1.Items[i].FindControl("label2")).Text;
                string stuadd = ((Label)DataList1.Items[i].FindControl("label3")).Text;
                SqlConnection cn = new SqlConnection(strcon);
                SqlCommand cmd = new SqlCommand("insert into s(StuID,StuName,Stuaddurl) values('"+stuid+"','"+stuname+"','"+stuadd+"')", cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("delete from students where ID='"+id+"'",cn);
                cmd1.ExecuteNonQuery();
                cn.Close();
               
            }
        
        }
        bind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            if (((CheckBox)DataList1.Items[i].FindControl("select")).Checked == true)
            {
                int id = Convert.ToInt32(DataList1.DataKeys[i].ToString());
                int stuid = Convert.ToInt32(((Label)DataList1.Items[i].FindControl("label1")).Text);
                string stuname = ((Label)DataList1.Items[i].FindControl("label2")).Text;
                string stuadd = ((Label)DataList1.Items[i].FindControl("label3")).Text;
                SqlConnection cn = new SqlConnection(strcon);
                SqlCommand cmd = new SqlCommand("insert into sj(stuid,stuname,stuadd) values('" + stuid + "','" + stuname + "','" + stuadd + "')", cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("delete from students where ID='"+id+"'",cn);
                cmd1.ExecuteNonQuery();
                cn.Close();
              
            }
        }
        bind();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            if (((CheckBox)DataList1.Items[i].FindControl("select")).Checked )
            {
                int id = Convert.ToInt32(DataList1.DataKeys[i].ToString());
                SqlConnection cn = new SqlConnection(strcon);
                SqlCommand cmd = new SqlCommand("delete from students where ID='"+id+"'",cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }
        }
        bind();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        int stuid = Convert.ToInt32(TextBox1.Text);
        string stuname=TextBox2.Text;
        string stuadd = TextBox3.Text;
        SqlConnection cn = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("insert into students(StuID,Stuname,Stuadduml) values('"+stuid+"','"+stuname+"','"+stuadd+"')", cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        bind();
    }
}
                                                     
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值