ASP.NET—002:GridView手动增加一行

发布时间:2020-06-15 23:31:38 作者:yangyoushan
来源:网络 阅读:628

ASP.NET中的gridview如何增加一行呢,下面介绍一种最简单的方式。
只使用后台的数据,在后台的datatable或者list增加一项,然后重新绑定gridview。
直接看代码
效果:
ASP.NET—002:GridView手动增加一行
实体类

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; }
        }
    }


页面管理类
public class MainManager
    {
        private int sIndex = 0;


        private List<PersonModel> personCollect = new List<PersonModel>();


        private static MainManager instance = null;


        public List<PersonModel> PersonCollect
        {
            get { return personCollect; }


            set { personCollect = value; }
        }


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


            return instance;
        }


        public void DoAddSinglePersons()
        {
            sIndex = this.PersonCollect.Count;


            PersonModel model = new PersonModel();


            model.PersonIndex = sIndex;


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


            model.PersonName = "测试" + sIndex;


            model.PersonAge = 17 + sIndex;


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


            model.PersonSelected = false;


            this.PersonCollect.Add(model);
        }
    }


前台
<head runat="server">
    <title></title>
</head>
<body id="MyBody" runat="server" ms_positioning="GridLayout">
    <form id="Form1" runat="server" method="post">
    <div>
    <asp:Button ID="btnAdd" runat="server" OnClick="AddItem_Click" Text="增加" />
    <asp:GridView ID="dgPersons" runat="server" AutoGenerateColumns="False" 
     EnableViewState="false"
    CellPadding="4"  ForeColor="#333333" DataKeyNames="PersonID">
     <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>


后台
public partial class _Default : System.Web.UI.Page
    {
        private MainManager dManager = null;


        protected PersonModel selectItem = null;


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


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


                this.dgPersons.DataSource = dManager.PersonCollect;


                this.dgPersons.DataBind();
            }
        }


        protected void AddItem_Click(object sender, EventArgs e)
        {
            dManager.DoAddSinglePersons();


            this.dgPersons.DataSource = dManager.PersonCollect;


            this.dgPersons.DataBind();
        }
    }
如果一行中有其他list控件比如 下拉列表asp:DropDownList,那么在实体中增加一个list,对应下拉列表的绑定。而且对于每一行的点击,选择等操作都要写相应的事件修改数据,下一次绑定时要将list中的数据修改成操作过的值或状态,再进行绑定。
当然一般来说,使用js增加比较正规一些,下一篇来介绍。
代码下载


推荐阅读:
  1. ASP.NET—006:GridView删除一行JS实现
  2. ASP.NET如何实现GridView增加一行JS

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net gridview 手动增加

上一篇:【前端】Neo4j前端源代码阅读笔记

下一篇:企业网络如何进行IP-MAC绑定?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》