GridView里的按钮事件
问题参考:
下面是Insus.NET实现演示:
CObj.cs代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
//
}
}
}
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代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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="+" />
<asp:Button ID="ButtonSubtract" runat="server" Text="-" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<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="+" />
<asp:Button ID="ButtonSubtract" runat="server" Text="-" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
引用 using Insus.NET;
xxx.aspx.cs代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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();
}
{
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添加二笔记录,分别测试每一行效果: