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);
    }
  • 相关阅读:
    宝宝打疫苗
    【小工具】2. 需要对测试用的数据进行MD5加密
    【小工具】1.需要对txt存放的测试数据做去重处理
    【Jenkins】定时构建语法
    【bug】记一个有趣的“bug”
    1.由于测试某个功能,需要生成500W条数据的txt,python代码如下
    开发基于vue前端框架下的系统的UI自动化,记录总结踩的坑
    使用Chrome-headless模式下,截屏不全屏的问题
    Chrome-headless 模式,没有UI界面的自动化UI测试
    【selenium】Webdriver的原理以及工作流程
  • 原文地址:https://www.cnblogs.com/ellafive/p/13328896.html
Copyright © 2011-2022 走看看