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);
}
原文地址:https://www.cnblogs.com/ellafive/p/13328896.html