第六章 用户权限管理(三) - DataGrid控件技巧

本文介绍了在ASP.NET中使用DataGrid控件进行资源管理的实现方式,包括资源的添加、删除、编辑和权限验证。后台代码展示了如何绑定数据、处理异常信息以及与数据库的交互,涉及到Session验证、SQL查询和视图状态的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6.5 资源管理的设计与实现

6.5.2 资源管理的程序实现   

    以下是资源管理页面Resource.aspx的后台代码(Resource.aspx.cs):

    ......

    using DataAccess;

    namespace workflow.admin
    {

      public class Resource : System.Web.UI.Page
      {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.DropDownList DropDownList1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
 
        private void Page_Load(object sender, System.EventArgs e)
        {
          if(!IsPostBack)
          {
              //验证用户是否登录
              if(Session["userid"] == null)
              Response.Redirect("./Message.aspx");
              BindGrid();
              BindList();
          }

          //span控件,显示程序异常信息  
          Message.InnerHtml="";

          //用于显示程序异常信息
          Label1.Text="";
       }

       //设置DataGrid控件的数据源并绑定数据

       void BindGrid()
       {
           string strSql="select *,isnull((select CategoryName from ResourceCategories  where

                    CategoryID=(select InheritedCategoryID from ResourceCategories where

                     CategoryID=ResourceType))+':','')+isnull((select CategoryName from

                       ResourceCategories  where CategoryID=ResourceType),'') as

                         fullcategoryname from Resource order by ResourceType";

           //Sql语句说明:  子句是在资源分类表中获取特定资源类的父类,其中ResourceType是资源表

           //Resource当前记录的资源类别编号;两个isnull()子句分别获得资源的父类和类别名称
           DataSet ds=new Base().SQLExeDataSet(strSql);
           if(ds !=null)
           {
              DataGrid1.DataSource=ds;
              DataGrid1.DataBind();

              //这里用viewstate存储数据集,如果用普通变量,当aspx页面重新加载后,上一次存放在

              //变量中的数据就会丢失。视图状态viewstate类似静态变量,是存储在客户端的隐藏信息,

              //当服务器接受页面回传时将访问viewstate内容。服务器端控件的属性中都有viewstate,

              //只需将其设置为true即可,就可以保存控件信息.
              ViewState["datasource"]=ds;
           }
        }

        //DropDownList1控件绑定数据

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值