GridView绑定List、页面返回值的实现方法-创新互联

本篇内容主要讲解“GridView绑定List、页面返回值的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“GridView绑定List、页面返回值的实现方法”吧!

海安ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

用惯了WPF的绑定,ASP.NET的绑定貌似不是很好用。下面看看ASP.NET绑定的用法。一般来说可以直接绑定DataTable的,不过我觉得绑定List比较符合面向对象编程。
绑定的方法是两句代码:


复制代码 代码如下:


GridView名.DataSource = List<自定义类>; 

GridView名.DataBind();


直接看例子吧,以下是一个绑定一个PersonModel类的例子。其中用到了页面返回参数,使用js传递,js可写在前端也可直接写在后台代码里。
项目结构:
GridView绑定List、页面返回值的实现方法

效果:
GridView绑定List、页面返回值的实现方法

实体类


复制代码 代码如下:


public class PersonModel
    {
        private int personIndex;


        public int PersonIndex
        {
            get { return personIndex; }


            set { personIndex = value; }
        }


        private string personID;


        public string PersonID
        {
            get { return personID; }


            set { personID = value; }
        }


        private string personName;


        public string PersonName
        {
            get { return personName; }


            set { personName = value; }
        }


        private string personSex;


        public string PersonSex
        {
            get { return personSex; }


            set { personSex = value; }
        }


        private int personAge;


        public int PersonAge
        {
            get { return personAge; }


            set { personAge = value; }
        }


        private bool personSelected = false;


        public bool PersonSelected
        {
            get { return personSelected; }


            set { personSelected = value; }
        }
    }


针对绑定的aspx页面写一个管理类,用于操作数据


复制代码 代码如下:


public class ChildFrmManager
    {
        private List personCollect = new List();


        private static ChildFrmManager instance = null;


        public List PersonCollect
        {
            get { return personCollect; }


            set { personCollect = value; }
        }


        public static ChildFrmManager DoGetInstance()
        {
            if (instance == null)
            {
                instance = new ChildFrmManager();
            }


            return instance;
        }


        public void DoAddPersons()
        {
            for (int i = 0; i < 20; i++)
            {
                PersonModel model = new PersonModel();


                model.PersonIndex = i + 1;


                model.PersonID = System.Guid.NewGuid().ToString();


                model.PersonName = "测试" + i;


                model.PersonAge = 27 + i;


                model.PersonSex = i % 2 == 0 ? "男" : "女";


                model.PersonSelected = false;


                this.PersonCollect.Add(model);
            }
        }
    }


绑定的页面前端


复制代码 代码如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChildFrm.aspx.cs" Inherits="ASPNetGridView.Pages.ChildFrm" %>





   

<
   


   

         EnableViewState="false"
    CellPadding="4"  ForeColor="#333333" DataKeyNames="PersonID"
     OnSelectedIndexChanged="Selcted_Click">
    
        
      
      
      
      
      

      

      
      
      
    

       
           
           
           
           
           

   

   

   



绑定页面后台


复制代码 代码如下:


 public partial class ChildFrm : System.Web.UI.Page
    {
        private ChildFrmManager dManager = null;


        protected PersonModel selectItem = null;


        protected void Page_Load(object sender, EventArgs e)
        {
            dManager = ChildFrmManager.DoGetInstance();


            if (!IsPostBack)
            {
                dManager.DoAddPersons();


                this.dgPersons.DataSource = dManager.PersonCollect;


                this.dgPersons.DataBind();
            }
        }


        protected void Selcted_Click(object sender, EventArgs e)
        {
            int selectIndex = this.dgPersons.SelectedIndex;


            foreach (PersonModel mitem in dManager.PersonCollect)
            {
                if (mitem.PersonIndex - 1 == selectIndex)
                {
                    mitem.PersonSelected = true;
                }
                else
                {
                    mitem.PersonSelected = false;
                }
            }


            selectItem = dManager.PersonCollect[selectIndex];


            string vbCrLf = " ";


            string strScript = "" + vbCrLf;


            if (!IsClientScriptBlockRegistered("clientScript"))
            {
                RegisterClientScriptBlock("clientScript", strScript);
            }
        }
    }


承载绑定页面的页面


复制代码 代码如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrameFrm.aspx.cs" Inherits="ASPNetGridView.Pages.FrameFrm" %>





   


   
   
 


主页面,获取返回值的js在前端


复制代码 代码如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNetGridView._Default" %>





   


   


       

           
             
                 


                 


                 
             
           

                  选择结果
                 

                 
                 

                 
                 

       

   



到此,相信大家对“GridView绑定List、页面返回值的实现方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前名称:GridView绑定List、页面返回值的实现方法-创新互联
标题URL:http://scyanting.com/article/cchdie.html