• GridView里的按钮事件


    问题参考:

    下面是Insus.NET实现演示:

    CObj.cs代码:

    View Code
    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代码:

    View Code
    <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;

     

    xxx.aspx.cs代码:

    View Code
    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();
        }

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

  • 相关阅读:
    CMake 用法导览
    Irrlicht 1.8.4 + Win7 + VC2015 + x64 +OpenGL编译
    VirtualBox 5.1.14 获取VirtualBox COM对象错误
    CGAL Manual/tutorial_hello_world.html
    CGAL 介绍
    Open CASCADE 基础类(Foundation Classes)
    OpenCASCADE 基础
    Nginx 反向代理详解
    修改docker容器中的hosts文件
    Jmeter 设置连接oracle数据库
  • 原文地址:https://www.cnblogs.com/insus/p/2697862.html
Copyright © 2020-2023  润新知