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"%>
 3DOCTYPE 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>
 8head>
 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>
34body>
35html>
36
37

 后台代码

 


  1using System;
  2using System.Web.UI.WebControls;
  3using Abin.Controls;
  4
  5public partial class Default3 : System.Web.UI.Page
  6{
  7    Page_load
 16
 17    DataGrid绑定
 25
 26    GetResult
 47
 48    DataGrid1_ItemCommand
 73
 74
 75}

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

 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
 3DOCTYPE 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>
 8head>
 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>
33body>
34html>
35
36

 后台代码

 

Code
 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.Page
13 {
14    Page_loadPage_load
23
24    DataGrid绑定DataGrid绑定
32
33    GetResultGetResult
54
55    TextBox1_TextChangedTextBox1_TextChanged
85}

86
87

 

完整的代码下载
http://www.cnblogs.com/Files/mextb1860/WebSite2.zip

你可能感兴趣的:(Asp.net,2003)