datalist gridview编辑.删除多行

来源:互联网 发布:弗兮网络 编辑:程序博客网 时间:2024/05/14 16:05
cs部分:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            for (int i = 1; i <=GridView1.Columns.Count; i++)
            {
                DropDownList1.Items.Add(i.ToString());
            }
        }
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();
      
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);

        string username = ((TextBox)e.Item.FindControl("TextBox1")).Text;
        string pwd = ((TextBox)e.Item.FindControl("TextBox2")).Text;



        if (DB.exesql("update login set username='"+username+"',userpwd='"+pwd+"' where id="+id) > 0)
        {

           // Response.Write("<script>alert('修改成功!');</script>");

            DataList1.EditItemIndex = -1;

            DataList1.DataBind();

        
        }
    }
    
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.Item.ItemIndex);

       if(DB.exesql("delete from login where id="+id)>0)

       {
           DataList1.EditItemIndex = -1;
           DataList1.DataBind();
      
       }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
      
      
    
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int c = Convert.ToInt32(DropDownList1.SelectedValue)-1;

        GridView1.Columns[c].Visible = false;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Columns.Count;i++ )
        {
            GridView1.Columns[i].Visible = true;
        }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
         foreach( GridViewRow dr in GridView1.Rows )
         {
             CheckBox chk = (CheckBox)dr.Cells[3].FindControl("CheckBox2");
             if (!chk.Checked)
             {
                 chk.Checked = true;
             }
             else
             {
                 chk.Checked = false;
            
             }
         }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        foreach( GridViewRow gr in GridView1.Rows  )
        {
            CheckBox chk = (CheckBox)gr.Cells[3].FindControl("CheckBox2");

            if (chk.Checked)
            {
                int UserID = Convert.ToInt32(((HiddenField)gr.Cells[4].FindControl("HiddenField1")).Value);

                DB.exesql("delete from ygb where yg_id=" + UserID);
                GridView1.DataBind();
                Response.Write("删除成功!");
                
            }
        }
    }
}
 
原创粉丝点击