zoukankan      html  css  js  c++  java
  • Gridview裡面的按鈕事件

    https://www.cnblogs.com/insus/archive/2012/09/22/2697862.html

    https://www.cnblogs.com/insus/archive/2011/06/30/2094151.html

    CObj.cs代码:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    ///


    /// Summary description for CObj
    ///

    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">

    asp:TemplateField

    key


    <%# Eval("Nbr") %>

    </asp:TemplateField>
    asp:TemplateField

    Value


    <asp:Label ID="LabelVal" runat="server" Text='<%# Eval("Val") %>'></asp:Label>

    </asp:TemplateField>
    asp:TemplateField

    operator


    <asp:Button ID="ButtonAdd" runat="server" Text="+" />  
    <asp:Button ID="ButtonSubtract" runat="server" Text="-" />

    </asp:TemplateField>

    </asp:GridView>

    引用 using Insus.NET;

    xxx.aspx.cs代码:

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

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="MediaTypeId"
    OnRowCreated="GridView1_RowCreated">


    <asp:TemplateField HeaderText="Select">

    <asp:Button ID="Button1" runat="server" Text="选择" />

    </asp:TemplateField>

    </asp:GridView>

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

        if (e.Row.FindControl ("Button1") != null)
        {
            Button CtlButton = (Button)e.Row.FindControl ("Button1");
            CtlButton.Click +=new EventHandler(CtlButton_Click);
        }
    }
    
    private void CtlButton_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        GridViewRow gvr = (GridViewRow)button.Parent.Parent;
        string pk = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
    
        //do something
    
        //InsusJsUtility objJs = new InsusJsUtility();  //http://www.cnblogs.com/insus/articles/1341703.html
        //objJs.JsAlert(pk);
    }
  • 相关阅读:
    C++雾中风景14:CRTP, 模板的黑魔法
    ClickHouse源码笔记1:聚合函数的实现
    C++雾中风景番外篇4:GCC升级二三事
    C++雾中风景13:volatile解惑
    AeroSpike踩坑手记1:Architecture of a Real Time Operational DBMS论文导读
    Linux 程序设计1:深入浅出 Linux 共享内存
    C++雾中风景番外篇3:GDB与Valgrind ,调试代码内存的工具
    C++雾中风景番外篇2:Gtest 与 Gmock,聊聊C++的单元测试
    C++雾中风景12:聊聊C++中的Mutex,以及拯救生产力的Boost
    用TensorFlow搭建一个万能的神经网络框架(持续更新)
  • 原文地址:https://www.cnblogs.com/ellafive/p/13328896.html
Copyright © 2011-2022 走看看