Default.aspx

View Code
<%@ 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>
    
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
            DataKeyNames="UserID" Font-Size="13px" 
            OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowDataBound="GridView1_RowDataBound" PageSize="6" SkinID="gvSkin" 
            Width="33%">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelected" runat="server" Checked="False" Visible="True" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                                <%# Container.DataItemIndex+1 %>
                            </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="编号(或用户名)">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("UserID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="真实姓名">
                    <EditItemTemplate>
                        <asp:TextBox ID="tbxName" runat="server" Text='<%# Eval("UserName") %>' 
                            Width="70px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server"><%# Eval("UserName") %></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="系别">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlDepartment" runat="server" Width="80px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server"><%# Eval("DepartmentName") %></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="角色">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlRole" runat="server" Width="80px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server"><%# Eval("RoleName") %></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" 
            OnCheckedChanged="chkSelectAll_CheckedChanged" Text="全选" Width="54px" />
        <asp:ImageButton ID="ImageButtonResetPassword" runat="server" 
            ImageUrl="~/重密码.gif" OnClick="ImageButtonResetPassword_Click" />
        <asp:Label ID="LabelPageInfo" runat="server"></asp:Label>
        <br />
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    public SqlDb sqldb = new SqlDb();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewBind();
        }
        ImageButtonResetPassword.Attributes.Add("onClick", "javascript:return confirm('确定重置密码?');");
    }
    private void GridViewBind()
    {
        string strsql = "SELECT Usersmr.UserID,    Usersmr.UserName,Departmentmr.DepartmentName,Rolemr.RoleName FROM Usersmr,Departmentmr,Rolemr WHERE Usersmr.DepartmentId=Departmentmr.DepartmentId AND Usersmr.RoleId=Rolemr.RoleId";
        DataSet myds = sqldb.ExceDS(strsql);
        GridView1.DataSource = myds;//为GridView控件指名数据源
        GridView1.DataBind();//GridView控件绑定数据
        LabelPageInfo.Text = "你所在的位置:当前是:(第" + (GridView1.PageIndex + 1).ToString() + "页/共" + GridView1.PageCount.ToString() + "页)";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((DropDownList)e.Row.FindControl("ddlRole")) != null)
        {
            DropDownList myddlrole = (DropDownList)e.Row.FindControl("ddlRole");
            //生成 DropDownList 的值,绑定数据
            string strsql = "SELECT Rolemr.RoleId,Rolemr.RoleName FROM Rolemr";
            DataSet myds = sqldb.ExceDS(strsql);
            myddlrole.DataSource = myds.Tables[0].DefaultView;
            myddlrole.DataTextField = "RoleName";
            myddlrole.DataValueField = "RoleId";
            myddlrole.DataBind();
        }
        if (((DropDownList)e.Row.FindControl("ddlDepartment")) != null)
        {
            DropDownList ddldepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
            //  生成 DropDownList 的值,绑定数据
            string sqlstr = "SELECT DepartmentId,DepartmentName FROM Departmentmr";
            DataSet myds = sqldb.ExceDS(sqlstr);
            ddldepartment.DataSource = myds.Tables[0].DefaultView;
            ddldepartment.DataTextField = "DepartmentName";
            ddldepartment.DataValueField = "DepartmentId";
            ddldepartment.DataBind();
        }
        int i;
        //执行循环,保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='Aqua'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    protected void ImageButtonResetPassword_Click(object sender, ImageClickEventArgs e)
    {
        int numOfChecked = 0;
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("chkSelected")).Checked;
            if (isChecked)
            {
                numOfChecked++;
            }
        }
        if (numOfChecked == 1)
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);//创建数据库连接
            SqlCommand sqlcom;                            //创建命令对象变量
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("chkSelected")).Checked;
                if (isChecked)
                {
                    string UserID = ((Label)GridView1.Rows[i].FindControl("Label1")).Text;
                    Random ran = new Random();
                    string newPassword = (ran.Next(999999).ToString().PadLeft(6, '8'));    //随机生成一个密码
                    string pwdMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword, "MD5").ToString();
                    string strsql = "UPDATE Usersmr SET UserPwd = @UserPwd WHERE (UserID = @UserID)";
                    if (sqlcon.State.Equals(ConnectionState.Closed))
                        sqlcon.Open();                        //打开数据加连接
                    sqlcom = new SqlCommand(strsql, sqlcon);
                    SqlParameter[] prams = {
                          new SqlParameter("@UserPwd",SqlDbType.VarChar,50),
                          new SqlParameter("@UserID",SqlDbType.VarChar,50)
                      };
                    prams[0].Value = pwdMd5.ToString().Trim();
                    prams[1].Value = GridView1.DataKeys[i].Value;
                    foreach (SqlParameter parameter in prams)
                    {
                        sqlcom.Parameters.Add(parameter);
                    }
                    if (sqlcom.ExecuteNonQuery() > 0)//更改用户密码
                    {
                        Page.RegisterStartupScript("","<Script language=JavaScript>alert('" + UserID + "的密码已经重置,新密码为【" + newPassword + "】。');location='Default.aspx';</Script>");
                    }
                    else
                    {
                        Response.Write("<Script language=JavaScript>alert('" + UserID + "重置密码失败!');location='Default.aspx';</Script>");//修改密码失败
                    }
                    sqlcon.Close();
                    sqlcom.Dispose();
                }
                else
                {
                    isChecked = false;//取消选中
                }
            }

        }
        else
        {
            Response.Write("<Script language=JavaScript>alert('您只能选择一个用户!');;location='Default.aspx';</Script>");
            return;
        }
    }
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            ((CheckBox)GridView1.Rows[i].FindControl("chkSelected")).Checked = this.chkSelectAll.Checked;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
}

 

 

posted on 2013-03-11 03:15  松竹梅  阅读(641)  评论(0编辑  收藏  举报