zoukankan      html  css  js  c++  java
  • 2017-5-22 Asp.Net内置对象2和RepeaterCommand用法

    (一)内置对象

    1.Application  

      储存在服务端,占用服务器内存,生命周期永久

      所有人访问的都是一个对象

      用法:页面之间额传值

    protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            
            Application["aa"] = TextBox1.Text;
            Response.Redirect("Default2.aspx");
        }

      接收页面:

       Label1.Text = Application["aa"].ToString();

      对象特征:

        存储的物理位置:服务器内存。

         存储的类型限制:任意类型。

         状态使用的范围:整个应用程序。

         存储的大小限制:任意大小。

         生命周期:应用程序开始的时候创建(准确来说是用户第一次请求某URL的时候创建),应用程序结束的时候销毁。

         安全与性能:数据总是存储在服务端,安全性比较高,但不易存储过多数据。

         优缺点与注意事项:检索数据速度快,但缺乏自我管理机制,数据不会自动释放。

      常用的属性和变量:

      

    All 返回全部的Application对象变量到一个对象数组
    AllKeys 返回全部的Application对象变量到一个字符串数组
    Count 取得Application中对象变量的数量
    Item Application变量名称传回的内容值
    Add 新增一个Application变量值
    Clear 清空全部Application变量值
    Get 变量名传回的变量值
    Set 更新Application变量值
    Lock 锁定所有Application变量值
    UnLock 解除锁定Application变量

    2.ViewState

      用于记录页面的一些Value,

      GET:URL?表单元素Name=表单元素Value&表单元素Name=表单元素Value

      webform中微软已经给我们做好了这个对象,自带此功能

    (二)RepeaterCommand事件用法

    1.在要触发的事件中添加属性CommandName="";方便在后台代码中区分点击的事件

    CommandArgument='<%#Eval("ids") %>'   传递的参数,在后台代码通过e.CommandArgument;获取传递的参数

      Repeater里面如果循环控件,控件的ID是会被改变的
      ItemCommand事件 - 任何控件执行提交都来触发这个事件  
      属性 - CommandName=""
      属性 - CommandArgument="主键值"

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Users> ulist = new UsersData().selectAll();
                Repeater1.DataSource = ulist;
                Repeater1.DataBind();
            }
            Repeater1.ItemCommand += Repeater1_ItemCommand;
        }
    
        void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //触发修改要操作的
            if (e.CommandName == "update")
            {
                Label1.Text = "修改";
    
            }
            //触发删除要操作的
            else if(e.CommandName=="delete")
            {
                Label1.Text = "删除";
            }
        }

     2.Repeater控件中:数据创建的时候执行的事件 ItemCreated,数据绑定的时候执行的事件 ItemDataBound

    ItemCreated - 绑定数据之前,创建行之后      ItemDataBound - 绑定数据之后,执行一遍

    用法:

     protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.ItemCommand += Repeater1_ItemCommand;
            //数据创建的时候
            Repeater1.ItemCreated += Repeater1_ItemCreated;
            //数据绑定的时候
            Repeater1.ItemDataBound += Repeater1_ItemDataBound;
            if (!IsPostBack)
            {
                List<Users> ulist = new UsersData().selectAll();
                Repeater1.DataSource = ulist;
                Repeater1.DataBind();
            }
           
        }
        //数据在绑定的时候执行,如果性别为女的不显示
        void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem ri = e.Item;
            Users u = ri.DataItem as Users;
            if (u.Sex == false)
            {
                ri.Visible = false;
            }
        }
  • 相关阅读:
    Python-流程控制之if判断
    Python-流程控制之循环
    Python-基本运算符
    Python-基本运算符
    Python-数据类型的基本使用
    python2中与用户交互
    Python-内存管理
    vue 替换表格中的数据
    实现单例的三个方法
    django----框架介绍
  • 原文地址:https://www.cnblogs.com/qingnianxu/p/6890320.html
Copyright © 2011-2022 走看看