当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET―001:GridView绑定List、页面返回值具体实现

ASP.NET―001:GridView绑定List、页面返回值具体实现

2017年12月12日  | 移动技术网IT编程  | 我要评论

四川省乐至实验中学,米静婕,二胡凤阳花鼓

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

复制代码 代码如下:

gridview名.datasource = list<自定义类>; 

gridview名.databind(); 

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

效果:

实体类

复制代码 代码如下:

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<personmodel> personcollect = new list<personmodel>();


        private static childfrmmanager instance = null;


        public list<personmodel> 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" %>


<!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 id="mybody" runat="server" ms_positioning="gridlayout">
    <form id="form1" runat="server" method="post">
    <div>
    <asp:gridview id="dgpersons" runat="server" autogeneratecolumns="false"
     enableviewstate="false"
    cellpadding="4"  forecolor="#333333" datakeynames="personid"
     onselectedindexchanged="selcted_click">
     <columns>
       <asp:commandfield showselectbutton="true" /> 
       <asp:boundfield datafield="personindex" headertext="序号"/>
       <asp:templatefield>
       <itemtemplate>
       <input id="radiobutton1" name="pselect" type="radio" />
       </itemtemplate>
       </asp:templatefield>
       <asp:boundfield datafield="personname" headertext="姓名" />
       <asp:boundfield datafield="personage" headertext="年龄" />
       <asp:boundfield datafield="personsex" headertext="性别" />
     </columns>
        <footerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" />
            <pagerstyle backcolor="#284775" forecolor="white" horizontalalign="center" />
            <selectedrowstyle backcolor="#e2ded6" font-bold="true" forecolor="#333333" />
            <headerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" />
            <editrowstyle backcolor="#999999" />
            <alternatingrowstyle backcolor="white" forecolor="#284775" />

    </asp:gridview>
    </div>
    </form>
</body>
</html>

绑定页面后台

复制代码 代码如下:

 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 = "<script>" + vbcrlf;


            strscript += "window.parent.returnvalue='" + selectitem.personname + "';" + vbcrlf;


            strscript += "window.parent.close();" + vbcrlf;


            strscript += "</script>" + vbcrlf;


            if (!isclientscriptblockregistered("clientscript"))
            {
                registerclientscriptblock("clientscript", strscript);
            }
        }
    }

承载绑定页面的页面

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="framefrm.aspx.cs" inherits="aspnetgridview.pages.framefrm" %>


<!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>
<frameset rows="0,*">
    <frame src="about:blank">
    <frame src="childfrm.aspx">
  </frameset>
</html>

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

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="aspnetgridview._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>
    <div align="center">
        <form id="form1" runat="server" method="post">
            <table runat="server">
              <tr>
                  <td>
                  <asp:label id="label1" runat="server" font-bold="true">选择结果</asp:label>
                  </td>


                  <td>
                  <asp:textbox id="txtshowreturnvalue" runat="server" width="100px" />
                  </td>
                  <td>
                  <asp:button id="btnopennewfrm" runat="server" text="选择" width="60px" onclientclick="opennewwindow()"/>
                  </td>
              </tr>
            </table>
        </form>
    </div>
</body>
<script type ='text/javascript'>
    function opennewwindow() {


        var str = window.showmodaldialog('pages/framefrm.aspx', document.form1.txtshowreturnvalue.value, 'dialogwidth=1000px;dialogheight=900px', 'scroll:yes');


        if (str != null)
        { document.form1.txtshowreturnvalue.value = str; }
    }
</script>
</html>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网