using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Admin_Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        object adminname = Session["adminname"];
        if (adminname == null)
        {
            Response.Redirect("~/Admin/Login.aspx");
            return;
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.EditIndex = -1;
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sql = "delete from t_user where userguid = @userguid";
        string userguid = this.GridView1.DataKeys[e.RowIndex].Value.ToString().Trim();
        SqlParameter sp_userguid = new SqlParameter("userguid", userguid);
        SqlParameter[] splist = new SqlParameter[] { sp_userguid };
        Dao.insertUpdateDeleteUser(sql, splist);
        this.GridView1.EditIndex = -1;
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // sds.UpdateCommand = "update t_user set u_name = @u_name, u_sex = @u_sex, u_birth = @u_birth, u_address = @u_address, u_idnumber = @u_idnumber where u_id = @u_id"
        //sds.UpdateParameters.Add("u_name", CType(Me.GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text.Trim())
        //sds.UpdateParameters.Add("u_sex", CType(Me.GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text.Trim())
        //sds.UpdateParameters.Add("u_birth", CType(Me.GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text.Trim())
        //sds.UpdateParameters.Add("u_address", CType(Me.GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text.Trim())
        //sds.UpdateParameters.Add("u_idnumber", CType(Me.GridView1.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text.Trim())
        //sds.UpdateParameters.Add("u_id", Me.GridView1.DataKeys(e.RowIndex).Value.ToString().Trim())
        //sds.Update()
        //Me.GridView1.EditIndex = -1
        string sql = "update t_user set username=@username, userpassword = @userpassword, a1 = @a1, a2 = @a2, a3 = @a3, a4 = @a4, b1 = @b1, b2 = @b2, b3 = @b3, b4 = @b4, c1 = @c1, c2 = @c2, c3 = @c3, c4 = @c4, d1 = @d1, d2 = @d2, d3 = @d3, d4 = @d4, lastalterdatetime = getdate() where userguid = @userguid";
        string username = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToLower().Trim();
        if (username.Length > 10)
        {
            Response.Write("<script>alert('用户名过长!');</script>");
            return;
        }
        string userpassword = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToLower().Trim();
        if (userpassword.Length > 10)
        {
            Response.Write("<script>alert('密码过长!');</script>");
            return;
        }
        bool a1 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Checked;
        bool a2 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked;
        bool a3 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Checked;
        bool a4 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Checked;
        bool b1 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Checked;
        bool b2 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Checked;
        bool b3 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Checked;
        bool b4 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Checked;
        bool c1 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0])).Checked;
        bool c2 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0])).Checked;
        bool c3 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0])).Checked;
        bool c4 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0])).Checked;
        bool d1 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0])).Checked;
        bool d2 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0])).Checked;
        bool d3 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0])).Checked;
        bool d4 = ((CheckBox)(this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0])).Checked;
        string userguid = this.GridView1.DataKeys[e.RowIndex].Value.ToString().Trim();
        //以上代码也可以用for循环遍历出来
        //string[] slist = { "a1","a2","a3","a4","b1","b2","b3","b4","c1","c2","c3","c4","d1","d2","d3","d4","userguid"};
        //for (int i = 1; i <= slist.Length; i++)
        //{
        //    slist[i] = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[i].Controls[0])).Text.ToLower().Trim();
        //}
        SqlParameter sp_username = new SqlParameter("username", username);
        SqlParameter sp_userpassword = new SqlParameter("userpassword", userpassword);
        SqlParameter sp_a1 = new SqlParameter("a1", a1);
        SqlParameter sp_a2 = new SqlParameter("a2", a2);
        SqlParameter sp_a3 = new SqlParameter("a3", a3);
        SqlParameter sp_a4 = new SqlParameter("a4", a4);
        SqlParameter sp_b1 = new SqlParameter("b1", b1);
        SqlParameter sp_b2 = new SqlParameter("b2", b2);
        SqlParameter sp_b3 = new SqlParameter("b3", b3);
        SqlParameter sp_b4 = new SqlParameter("b4", b4);
        SqlParameter sp_c1 = new SqlParameter("c1", c1);
        SqlParameter sp_c2 = new SqlParameter("c2", c2);
        SqlParameter sp_c3 = new SqlParameter("c3", c3);
        SqlParameter sp_c4 = new SqlParameter("c4", c4);
        SqlParameter sp_d1 = new SqlParameter("d1", d1);
        SqlParameter sp_d2 = new SqlParameter("d2", d2);
        SqlParameter sp_d3 = new SqlParameter("d3", d3);
        SqlParameter sp_d4 = new SqlParameter("d4", d4);
        SqlParameter sp_userguid = new SqlParameter("userguid", userguid);
        SqlParameter[] splist = new SqlParameter[] { sp_username, sp_userpassword, sp_a1, sp_a2, sp_a3, sp_a4, sp_b1, sp_b2, sp_b3, sp_b4, sp_c1, sp_c2, sp_c3, sp_c4, sp_d1, sp_d2, sp_d3, sp_d4, sp_userguid };
        Dao.insertUpdateDeleteUser(sql, splist);
        this.GridView1.EditIndex = -1;
    }

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        this.GridView1.DataKeyNames = new string[] { "userguid" };
        this.GridView1.AllowPaging = true;
        this.GridView1.AutoGenerateDeleteButton = true;
        this.GridView1.AutoGenerateEditButton = true;
        this.GridView1.AutoGenerateSelectButton = true;
        this.GridView1.DataSource = Dao.selectUser();
        this.GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
    }
}
12-25 02:00