DataGrid中TextBox的onChange事件提升

微软的控件功能很强,开发起来容易上手,可是需求总是不能满足的。所以我们为了满足不同需求,会重写一控件.
就比如 DataGrid中TextBox的onChange事件.DataGrid捕获不到,TextBox和Button不一样.Button有commandName属性,我们可以用commandName属性来区别触发的事件.如果要实现TextBox的onChange事件让DataGrid捕获,那就需要事件提升,现在有2种解决方案第一种,重写TextBox控件
前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Register Namespace ="Abin.Controls" TagPrefix="Abin"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
OnItemCommand="DataGrid1_ItemCommand">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<Abin:CommandTextBox ID="TextBox1" runat="server" CommandName="OnChange" AutoPostBack="True"></Abin:CommandTextBox>
</ItemTemplate>
<HeaderTemplate>
工号
</HeaderTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<Abin:CommandTextBox ID="TextBox2" runat="server" BackColor="LightGray" ReadOnly="True"></Abin:CommandTextBox>
</ItemTemplate>
<HeaderTemplate>
名字
</HeaderTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></div>
</form>
</body>
</html>

后台代码

using System;
using System.Web.UI.WebControls;
using Abin.Controls;

public partial class Default3 : System.Web.UI.Page
{
#region Page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataGrid();
}
}
#endregion

#region DataGrid绑定
private void BindDataGrid()
{
DataGrid1.DataSource = new string[] { "Abin", "Abin", "Abin", "Abin", "Abin", "Abin" };

DataGrid1.DataBind();
}
#endregion

#region GetResult
private string GetResult(string strName)
{
if (strName == "033221")
{
return "阿滨";
}
else if (strName == "033222")
{
return "小猪";
}
else if (strName.Length == 0)
{
return null;
}
else
{
return "沒有找到";
}
}
#endregion

#region DataGrid1_ItemCommand
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "OnChange")
{
string currenttxtName = null;

int index = e.Item.ItemIndex;

CommandTextBox ctb = this.DataGrid1.Items[index].FindControl("TextBox1") as CommandTextBox;

if (ctb != null)
{
currenttxtName = ctb.Text;
}

ctb = this.DataGrid1.Items[index].FindControl("TextBox2") as CommandTextBox;

if (ctb != null)
{
ctb.Text = GetResult(currenttxtName);
}
}
}
#endregion


}


重写TextBox类


using System;
using System.Web.UI.WebControls;
using System.ComponentModel;
/// <summary>
/// CommandTextBox 的摘要说明
/// </summary>
///
namespace Abin.Controls
{
public class CommandTextBox : TextBox
{
#region 属性
[Browsable(true)]
public string CommandName
{
get
{
return ViewState["CommandName"] == null ? string.Empty : ViewState["CommandName"].ToString();
}

set
{
ViewState["CommandName"] = value;
}
}

#endregion

#region 事件冒泡

protected virtual void OnCommand(CommandEventArgs e)
{
base.RaiseBubbleEvent(this, e);
}
#endregion

#region 重写

protected override void OnTextChanged(EventArgs e)
{
if (this.AutoPostBack)
{
CommandEventArgs args = new CommandEventArgs(this.CommandName, null);

OnCommand(args);
}
}

#endregion
}
}


这样就可以实现捕获了,现在我们用第2种方法来实现


前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</ItemTemplate>
<HeaderTemplate>
工号
</HeaderTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" BackColor="LightGray"></asp:TextBox>
</ItemTemplate>
<HeaderTemplate>
名字
</HeaderTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></div>
</form>
</body>
</html>

后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default4 : System.Web.UI.Page
{
#region Page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataGrid();
}
}
#endregion

#region DataGrid绑定
private void BindDataGrid()
{
DataGrid1.DataSource = new string[] { "Abin", "Abin", "Abin", "Abin", "Abin", "Abin" };

DataGrid1.DataBind();
}
#endregion

#region GetResult
private string GetResult(string strName)
{
if (strName == "033221")
{
return "阿滨";
}
else if (strName == "033222")
{
return "小猪";
}
else if (strName.Length == 0)
{
return null;
}
else
{
return "沒有找到";
}
}
#endregion

#region TextBox1_TextChanged
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;

if (tb != null)
{
TableCell cell = tb.Parent as TableCell;

if (cell != null)
{
DataGridItem dgi = cell.Parent as DataGridItem;

string currenttxtName = null;

int index = dgi.ItemIndex;

tb = DataGrid1.Items[index].FindControl("TextBox1") as TextBox;

if (tb != null)
{
TextBox tb1 = DataGrid1.Items[index].FindControl("TextBox2") as TextBox;

tb1.Text = GetResult(tb.Text);
}
}
}

}
#endregion
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值