[ZT]DataGrid中TextBox的onChange事件解决方法

 

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

 

Code
 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


后台代码

Code
  1using System;
  2using System.Web.UI.WebControls;
  3using Abin.Controls;
  4
  5public partial class Default3 : System.Web.UI.Page
  6{
  7    Page_load#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    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    #endregion

 25
 26    GetResult#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    DataGrid1_ItemCommand#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        属性#region 属性
 95        [Browsable(true)]
 96        public string CommandName
 97        {
 98            get
 99            {
100                return ViewState["CommandName"== null ? string.Empty : ViewState["CommandName"].ToString();
101            }

102
103            set
104            {
105                ViewState["CommandName"= value;
106            }

107        }

108
109        #endregion

110
111        事件冒泡#region 事件冒泡
112
113        protected virtual void OnCommand(CommandEventArgs e)
114        {
115            base.RaiseBubbleEvent(this, e);
116        }

117        #endregion

118
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        #endregion

132    }

133}

134
135
136
137

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


前台代码

Code
 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


后台代码

Code
 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    Page_load#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    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    #endregion

32
33    GetResult#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    TextBox1_TextChanged#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种方法都实现了。代码很简单。我就不讲解了.
完整的代码下载
http://files.cnblogs.com/mextb1860/WebSite2.zip

转载于:https://www.cnblogs.com/godwar/archive/2008/09/21/1295331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WPF MVVM,你可以使用命令模式来将一个按钮的点击事件绑定到ViewModel的一个方法。以下是在DataGrid使用按钮并绑定命令方法的步骤: 1. 在ViewModel创建一个ICommand属性,该属性将绑定到按钮的Command属性。可以使用RelayCommand等现有的ICommand实现,也可以自己实现ICommand接口。如下所示: ```csharp public class MyViewModel { public ICommand MyCommand { get; set; } public MyViewModel() { MyCommand = new RelayCommand(ExecuteMyCommand); } private void ExecuteMyCommand(object parameter) { // 在这里编写命令方法的代码 } } ``` 2. 在XAML,在DataGrid创建一个Button列,并将Button的Command属性绑定到ViewModel的命令属性。如下所示: ```xaml <DataGrid ...> <DataGrid.Columns> ... <DataGridTemplateColumn Header="Action"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Do Something" Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> ``` 注意,这里使用了RelativeSource来绑定到Window的DataContext,因为DataGridDataContext通常是绑定到ViewModel的。 3. 运行应用程序并单击按钮时,将调用ViewModel的ExecuteMyCommand方法。 请注意,如果你要在DataGrid使用按钮,可能需要使用DataGridTemplateColumn来创建一个自定义列。在该列,使用DataTemplate来定义Button的外观和行为,并将Button的Command属性绑定到ViewModel的命令属性。还可以将Button的CommandParameter属性绑定到DataGrid的当前行,以便在命令方法访问该行的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值