[ZT]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


后台代码


  1using System;
  2using System.Web.UI.WebControls;
  3using Abin.Controls;
  4
  5public partial class Default3 : System.Web.UI.Page
  6{
  7    #region Page_load
  8    protected void Page_Load(object sender, EventArgs e)
  9    {
 10        if (!IsPostBack)
 11        {
 12            BindDataGrid();
 13        }

 14    }

 15    #endregion

 16
 17    #region DataGrid绑定
 18    private void BindDataGrid()
 19    {
 20        DataGrid1.DataSource = new string[] "Abin""Abin""Abin""Abin""Abin""Abin" };
 21
 22        DataGrid1.DataBind();
 23    }

 24    #endregion

 25
 26    #region GetResult
 27    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        else
 42        {
 43            return "沒有找到";
 44        }

 45    }

 46    #endregion

 47
 48    #region DataGrid1_ItemCommand
 49    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    #endregion

 73
 74
 75}

 76
 77
 78
 79重写TextBox类
 80
 81
 82
 83using System;
 84using System.Web.UI.WebControls;
 85using System.ComponentModel;
 86/// <summary>
 87/// CommandTextBox 的摘要说明
 88/// </summary>
 89/// 

 90namespace Abin.Controls
 91{
 92    public class CommandTextBox : TextBox
 93    {
 94        属性
110
111        事件冒泡
118
119        重写
132    }

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


后台代码


 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Default4 : System.Web.UI.Page
13{
14    #region Page_load
15    protected void Page_Load(object sender, EventArgs e)
16    {
17        if (!IsPostBack)
18        {
19            BindDataGrid();
20        }

21    }

22    #endregion

23
24    #region DataGrid绑定
25    private void BindDataGrid()
26    {
27        DataGrid1.DataSource = new string[] "Abin""Abin""Abin""Abin""Abin""Abin" };
28
29        DataGrid1.DataBind();
30    }

31    #endregion

32
33    #region GetResult
34    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        else
49        {
50            return "沒有找到";
51        }

52    }

53    #endregion

54
55    #region TextBox1_TextChanged
56    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    #endregion

85}

86
87

 


2种方法都实现了。代码很简单。我就不讲解了.
完整的代码下载
https://files.cnblogs.com/mextb1860/WebSite2.zip
posted on 2008-09-21 17:21  巍巍边疆  阅读(1535)  评论(2编辑  收藏  举报