zoukankan      html  css  js  c++  java
  • GridView做加

    原文:http://www.cnblogs.com/insus/archive/2012/09/22/2697862.html

    下面是Insus.NET实现演示:

    CObj.cs代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for CObj
    /// </summary>
    namespace Insus.NET
    {
        public class CObj
        {
            private int _Nbr;
            private int _Val;
                    

            public int Nbr
            {
                get { return _Nbr; }
                set { _Nbr = value; }
            }

            public int Val
            {
                get { return _Val; }
                set { _Val = value; }
            }
            public CObj()
            {
                //
                // TODO: Add constructor logic here
                //
            }
        }
    }

    GridView代码:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="230px" OnRowCreated ="GridView1_RowCreated">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                key
                            </HeaderTemplate>
                            <ItemTemplate>
                                <%# Eval("Nbr") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Value
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="LabelVal" runat="server" Text='<%# Eval("Val") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                operator
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Button ID="ButtonAdd" runat="server" Text="+" />&nbsp;&nbsp;
                                <asp:Button ID="ButtonSubtract" runat="server" Text="-" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    引用  using Insus.NET;

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Binding();
            }
        }

        private void Data_Binding()
        {
            List<CObj> MyObj = new List<CObj>();

            CObj o = new CObj();
            o.Nbr = 1;
            o.Val = 100;
            MyObj.Add(o);

            this.GridView1.DataSource = MyObj;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            if (e.Row.FindControl("ButtonAdd") != null)
            {
                var ButtonAdd = e.Row.FindControl("ButtonAdd") as Button;
                ButtonAdd.Click += ButtonAdd_Click;
            }

            if (e.Row.FindControl("ButtonSubtract") != null)
            {
                var ButtonSubtract = e.Row.FindControl("ButtonSubtract") as Button;
                ButtonSubtract.Click += ButtonSubtract_Click;
            }
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
            GridViewRow gvr = (GridViewRow)button.Parent.Parent;
            var Label = (Label)this.GridView1.Rows[gvr.RowIndex].FindControl("LabelVal");
            int v = Convert.ToInt32(Label.Text);
            Label.Text = (v + 1).ToString();
        }

        private void ButtonSubtract_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
            GridViewRow gvr = (GridViewRow)button.Parent.Parent;
            var Label = (Label)this.GridView1.Rows[gvr.RowIndex].FindControl("LabelVal");
            int v = Convert.ToInt32(Label.Text);
            Label.Text = (v - 1).ToString();
        }

    xxx.aspx.cs代码:

     于 11:46分,Insus.NET添加二笔记录,分别测试每一行效果:

  • 相关阅读:
    Java设计模式(学习整理)---工厂模式
    Java Swing 使用总结(转载)
    Java-生成验证码图片(自定义内容,尺寸,路径)
    二维码(带有图片)的生成
    J2se中的声音---AudioPlayer
    文件的读取和写入(指定路径)
    ASP.NET:使用Flurl制作可复用的分页组件
    ASP.NET:Forms身份验证和基于Role的权限验证
    ASP.NET:MVC模板化机制
    ASP.NET:MVC中文件上传与地址变化处理
  • 原文地址:https://www.cnblogs.com/Echo529/p/6386419.html
Copyright © 2011-2022 走看看