DataGrid中TextBox的onChange事件提升
就比如DataGrid中TextBox的onChange事件.DataGrid捕获不到,TextBox和Button不一样.Button有commandName属性,我们可以用commandName属性来区别触发的事件.如果要实现TextBox的onChange事件让DataGrid捕获,那就需要事件提升,现在有2种解决方案第一种,重写TextBox控件
前台代码
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>2
<%@ Register Namespace ="Abin.Controls" TagPrefix="Abin"%>3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"13
OnItemCommand="DataGrid1_ItemCommand">14
<Columns>15
<asp:TemplateColumn>16
<ItemTemplate>17
<Abin:CommandTextBox ID="TextBox1" runat="server" CommandName="OnChange" AutoPostBack="True"></Abin:CommandTextBox>18
</ItemTemplate>19
<HeaderTemplate>20
工号21
</HeaderTemplate>22
</asp:TemplateColumn>23
<asp:TemplateColumn>24
<ItemTemplate>25
<Abin:CommandTextBox ID="TextBox2" runat="server" BackColor="LightGray" ReadOnly="True"></Abin:CommandTextBox>26
</ItemTemplate>27
<HeaderTemplate>28
名字29
</HeaderTemplate>30
</asp:TemplateColumn>31
</Columns>32
</asp:DataGrid></div>33
</form>34
</body>35
</html>36

37

后台代码
1
using System;2
using System.Web.UI.WebControls;3
using Abin.Controls;4

5
public partial class Default3 : System.Web.UI.Page6


{7

Page_load#region Page_load8
protected void Page_Load(object sender, EventArgs e)9

{10
if (!IsPostBack)11

{12
BindDataGrid();13
}14
}15
#endregion16

17

DataGrid绑定#region DataGrid绑定18
private void BindDataGrid()19

{20

DataGrid1.DataSource = new string[]
{ "Abin", "Abin", "Abin", "Abin", "Abin", "Abin" };21

22
DataGrid1.DataBind();23
}24
#endregion25

26

GetResult#region GetResult27
private string GetResult(string strName)28

{29
if (strName == "033221")30

{31
return "阿滨";32
}33
else if (strName == "033222")34

{35
return "小猪";36
}37
else if (strName.Length == 0)38

{39
return null;40
}41
else42

{43
return "沒有找到";44
}45
}46
#endregion47

48

DataGrid1_ItemCommand#region DataGrid1_ItemCommand49
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)50

{51
if (e.CommandName == "OnChange")52

{53
string currenttxtName = null;54

55
int index = e.Item.ItemIndex;56

57
CommandTextBox ctb = this.DataGrid1.Items[index].FindControl("TextBox1") as CommandTextBox;58
59
if (ctb != null)60

{61
currenttxtName = ctb.Text;62
}63

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

66
if (ctb != null)67

{68
ctb.Text = GetResult(currenttxtName);69
}70
}71
}72
#endregion73

74

75
}76

77

78

79
重写TextBox类80

81

82

83
using System;84
using System.Web.UI.WebControls;85
using System.ComponentModel;86

/**//// <summary>87
/// CommandTextBox 的摘要说明88
/// </summary>89
/// 90
namespace Abin.Controls91


{92
public class CommandTextBox : TextBox93

{94

属性#region 属性95
[Browsable(true)]96
public string CommandName97

{98
get99

{100
return ViewState["CommandName"] == null ? string.Empty : ViewState["CommandName"].ToString();101
}102

103
set104

{105
ViewState["CommandName"] = value;106
}107
}108

109
#endregion110

111

事件冒泡#region 事件冒泡112

113
protected virtual void OnCommand(CommandEventArgs e)114

{115
base.RaiseBubbleEvent(this, e);116
}117
#endregion118

119

重写#region 重写120
121
protected override void OnTextChanged(EventArgs e)122

{123
if (this.AutoPostBack)124

{125
CommandEventArgs args = new CommandEventArgs(this.CommandName, null);126

127
OnCommand(args);128
}129
}130

131
#endregion132
}133
}134

135

136

137

这样就可以实现捕获了,现在我们用第2种方法来实现
前台代码
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>2

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

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False">13
<Columns>14
<asp:TemplateColumn>15
<ItemTemplate>16
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>17
</ItemTemplate>18
<HeaderTemplate>19
工号20
</HeaderTemplate>21
</asp:TemplateColumn>22
<asp:TemplateColumn>23
<ItemTemplate>24
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" BackColor="LightGray"></asp:TextBox>25
</ItemTemplate>26
<HeaderTemplate>27
名字28
</HeaderTemplate>29
</asp:TemplateColumn>30
</Columns>31
</asp:DataGrid></div>32
</form>33
</body>34
</html>35

36

后台代码
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
public partial class Default4 : System.Web.UI.Page13


{14

Page_load#region Page_load15
protected void Page_Load(object sender, EventArgs e)16

{17
if (!IsPostBack)18

{19
BindDataGrid();20
}21
}22
#endregion23

24

DataGrid绑定#region DataGrid绑定25
private void BindDataGrid()26

{27

DataGrid1.DataSource = new string[]
{ "Abin", "Abin", "Abin", "Abin", "Abin", "Abin" };28

29
DataGrid1.DataBind();30
}31
#endregion32

33

GetResult#region GetResult34
private string GetResult(string strName)35

{36
if (strName == "033221")37

{38
return "阿滨";39
}40
else if (strName == "033222")41

{42
return "小猪";43
}44
else if (strName.Length == 0)45

{46
return null;47
}48
else49

{50
return "沒有找到";51
}52
}53
#endregion54

55

TextBox1_TextChanged#region TextBox1_TextChanged56
protected void TextBox1_TextChanged(object sender, EventArgs e)57

{58
TextBox tb = (TextBox)sender;59

60
if (tb != null)61

{62
TableCell cell = tb.Parent as TableCell;63

64
if (cell != null)65

{66
DataGridItem dgi = cell.Parent as DataGridItem;67

68
string currenttxtName = null;69

70
int index = dgi.ItemIndex;71

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

74
if (tb != null)75

{76
TextBox tb1 = DataGrid1.Items[index].FindControl("TextBox2") as TextBox;77

78
tb1.Text = GetResult(tb.Text);79
}80
}81
}82

83
}84
#endregion85
}86

87

2种方法都实现了。代码很简单。我就不讲解了.
完整的代码下载
https://files.cnblogs.com/mextb1860/WebSite2.zip

浙公网安备 33010602011771号