GridView.RowDataBound

using System;
using System.Collections;
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 MyOffice.BLL;
using MyOffice.Models;
using System.Collections.Generic;

public partial class SystemManage_RoleManage_RoleManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayRoleInfo();
        }
    }

    /*显示角色信息列表*/
    protected void DisplayRoleInfo()
    {
        //指定列表数据源
        gvRoleInfo.DataSource = RoleInfoManager.GetAllRoleInfo_Xu();
        //给列表绑定主键字段(表RoleInfo => RoleId)
        gvRoleInfo.DataKeyNames = new string[] { "RoleId"};
        //数据绑定
        gvRoleInfo.DataBind();
    }

    /*绑定时*/
    protected void gvRoleInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //得到当前行的主键(角色Id)
            string roleId = gvRoleInfo.DataKeys[e.Row.RowIndex].Value.ToString();

            /***找到当前行的“删除按钮”***/
            ImageButton imgBtnDelete = e.Row.FindControl("imgBtnDelete") as ImageButton;
            //指定按钮命令名
            imgBtnDelete.CommandName = "DeleteRole";
            //指定按钮关联参数
            imgBtnDelete.CommandArgument = roleId;
            //指定按钮特性
            imgBtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗?')");

            /***找到当前行的“修改按钮”***/
            ImageButton imgBtnEdit = e.Row.FindControl("imgBtnEdit") as ImageButton;
            //指定按钮命令名
            imgBtnEdit.CommandName = "EditRole";
            //指定按钮关联的参数
            imgBtnEdit.CommandArgument = roleId;

            /***找到当前行的“分配权限”链接***/
            HyperLink hlDistribute = e.Row.FindControl("hlDistribute") as HyperLink;
            //指定连接字体颜色
            hlDistribute.ControlStyle.ForeColor = System.Drawing.Color.Blue;
            //指定连接URL地址
            hlDistribute.NavigateUrl = "~/SystemManage/RoleManage/DistributeRole.aspx?roleId="+roleId;
        } 
    }

    /*执行时*/
    protected void gvRoleInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //如果是删除按钮
        if (e.CommandName == "DeleteRole")
        {
            int roleId = Convert.ToInt32(e.CommandArgument);
            RoleInfoManager.DeleteRoleInfoByRoleId_Xu(roleId);
            DisplayRoleInfo();
            //两个文本框清空
            txtRoleName.Text = "";
            txtRoleDesc.Text = "";
            //添加按钮可用、保存修改不可用
            btnAdd.Enabled = true;
            btnSave.Enabled = false;
        }

        //如果是修改按钮
        if (e.CommandName == "EditRole")
        {
            int roleId = Convert.ToInt32(e.CommandArgument);
            //找到该角色对象
            RoleInfo roleInfo = RoleInfoManager.GetRoleInfoById_Xu(roleId);
            //将信息显示到两个文本框上、RoleId放到
            hfRoleId.Value = roleInfo.RoleId.ToString();
            txtRoleName.Text = roleInfo.RoleName;
            txtRoleDesc.Text = roleInfo.RoleDesc;
            //添加按钮不可用、保存修改可用
            btnAdd.Enabled = false;
            btnSave.Enabled = true;
        }
    }

    /*添加一个角色信息*/
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //得到用户输入
        string roleName = txtRoleName.Text.ToString();
        string roleDesc = txtRoleDesc.Text.ToString();
        bool result = RoleInfoManager.CreateRoleInfo_Xu(roleName, roleDesc);
        //判断成功与否
        if (result)
        {
            //添加成功
            txtRoleName.Text = "";
            txtRoleDesc.Text = "";
            DisplayRoleInfo();
        }
        else
        {
            //提示用户已有角色名称
            Response.Write("<script>alert('该角色名称已经存在')</script>");
        }
    }

    /*保存修改*/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //得到用户的输入
        int roleId = Convert.ToInt32(hfRoleId.Value);
        string roleName = Convert.ToString(txtRoleName.Text);
        string roleDesc = Convert.ToString(txtRoleDesc.Text);
        //执行修改
        bool result = RoleInfoManager.ModifyRoleInfoByRoleId_Xu(roleId, roleName, roleDesc);
        //如果成功
        if (result)
        {
            Response.Write("<script>alert('修改成功')</script>");
            //两个文本框清空
            txtRoleName.Text = "";
            txtRoleDesc.Text = "";
            //添加按钮可用、保存修改不可用
            btnAdd.Enabled = true;
            btnSave.Enabled = false;
            DisplayRoleInfo();
        }
        else
        {
            Response.Write("<script>alert('该角色名称已经存在')</script>");
        }
    }
}

你可能感兴趣的:(GridView)