加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何将textchanged事件添加到代码隐藏中的文本框中

发布时间:2020-12-16 09:15:18 所属栏目:asp.Net 来源:网络整理
导读:我通过后面的代码创建了一个文本框,但无法向其添加文本更改的事件 这就是我 protected void insert(object sender,EventArgs e) {}protected void update(object sender,DayRenderEventArgs e) { TextBox tb = new TextBox(); tb.TextChanged += "insert"; e
我通过后面的代码创建了一个文本框,但无法向其添加文本更改的事件
这就是我

protected void insert(object sender,EventArgs e) 
{

}

protected void update(object sender,DayRenderEventArgs e) 
{
    TextBox tb = new TextBox();
    tb.TextChanged += "insert";

    e.Cell.Controls.Add(tb);

}

我试过这个,但它对我不起作用.
有什么问题,谢谢

解决方法

当您想要将处理程序绑定到代码隐藏中的事件时,您实际执行的操作是编写处理程序本身的名称,而不是字符串

protected void Page_Load(object sender,EventArgs e)
    {
        TextBox textBox = new TextBox();
        textBox.TextChanged += new EventHandler(textBox_TextChanged);
    }

    protected void textBox_TextChanged(object sender,EventArgs e)
    {
        // Your code here
    }

为了更清楚一点,想象一下C#有一个名为EventHandler的列表,每次文本框上的文本发生变化时(客户端的模糊事件),C#都会执行该列表中的所有方法.现在,如何在该列表中添加方法?你使用=运算符.现在,如果要添加两个处理程序,可以编写:

protected void Page_Load(object sender,EventArgs e)
    {
        TextBox textBox = new TextBox();
        textBox.TextChanged += new EventHandler(textBox_TextChanged);
        textBox.TextChanged += new EventHandler(textBox_TextChanged2);
    }

    protected void textBox_TextChanged(object sender,EventArgs e)
    {
        // This method is the first in the list. So gets executed first.
    }

    protected void textBox_TextChanged2(object sender,EventArgs e)
    {
        // This method is the second in the list.
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读