给动态添加的textbox加TextChanged事件为什么不触发?

dt891030 2011-01-04 04:04:57
我动态添加textbox的事件TextChanged, 为什么不能触发?本人新手望多多指教!

textbox.TextChanged += new EventHandler(this.tb_TextChanged);


源码如下:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class testpro : System.Web.UI.Page
{
public int[] arr = null;
public static ArrayList TextArray = new ArrayList();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtLength.Attributes.Add("dir", "rtl");
}
}

//protected override void OnPreLoad(EventArgs e)
//{
// base.OnPreLoad(e);
//}

protected void btnNew_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtLength.Value.Trim()))
{
plArray.Controls.Clear();
if (int.Parse(txtLength.Value.Trim()) != 0)
{
//plArray.Visible = true;
for (int i = 0; i < int.Parse(txtLength.Value.Trim()); i++)
{
TextBox textbox = new TextBox();
textbox.ID = "txtNum_" + (i + 1).ToString();
textbox.Width = 35;
textbox.Attributes.Add("dir", "rtl");
textbox.Attributes.Add("onkeypress", "javascript:KeyPress(this);");
textbox.MaxLength = 5;
textbox.AutoPostBack = true;
textbox.TextChanged += new EventHandler(this.tb_TextChanged);
textbox.Text = "0";
TextArray.Add(textbox);
plArray.Controls.Add((TextBox)TextArray[i]);
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入数组长度!');", true);

}
}

protected void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb != null)
{
Response.Write(tb.ID);
}
else
{
Response.Write("you want to die !");
}
}


protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb != null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + tb.ID + "!');", true);
}
else
{
Response.Write("you want to die !");
}
}
}




页面源码如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testpro.aspx.cs" Inherits="testpro" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>


<script language="javascript" type="text/javascript">
function KeyPress(objTR)
{//只允许录入数据字符 0-9 和小数点
//var objTR = element.document.activeElement;
var txtval=objTR.value;
var l=objTR.value.length;
var key = event.keyCode;
if((key < 48||key > 57)&&key != 46)
{
event.keyCode = 0;
}
else
{
if(key == 46)
{
if(txtval.indexOf(".") != -1||txtval.length == 0)
{
event.keyCode = 0;
}
}
}
if(txtval.indexOf(".") != -1)
{
var decimalPart=txtval.substring(txtval.indexOf(".")+1,l);
if(decimalPart.length>=2)
{
event.keyCode = 0;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="height:auto;margin-bottom:15px;">
数组长度:
<input type="text" id="txtLength" runat="server" onkeypress="javascript:KeyPress(this);"
maxlength="5" value="0" size="1" />
   
<asp:Button ID="btnNew" runat="server" OnClick="btnNew_Click" Text="声明" /> <span
style="color: Red; font-size: 12px;">[请不要输入小数]</span>
<asp:Panel ID="plArray" runat="server" Width="600px">
</asp:Panel>
<br />
 
</div>
<div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox></div>
</form>
</body>
</html>


...全文
562 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dt891030 2011-01-11
  • 打赏
  • 举报
回复
JS怎么处理呢~哪位大侠给下脚本
sshenry1151 2011-01-07
  • 打赏
  • 举报
回复
怎么用js处理啊,那位大侠有脚本啊?
bh_shoeleather 2011-01-05
  • 打赏
  • 举报
回复
建议楼主看看页面的生命周期,相信你会从中找到答案的。
lilianjie0426 2011-01-05
  • 打赏
  • 举报
回复
Page_load中也要写,你只写在按钮事件中,一回发页面重新加载,动态添加的按钮就没有了。。。
wangting0613 2011-01-04
  • 打赏
  • 举报
回复
放在页面初始化事件中试试,,oninit(),,它比onload事件先执行
koukoujiayi 2011-01-04
  • 打赏
  • 举报
回复
1.按钮事件中写!
2.Page_load的IsPostBack还要写!

不过我觉得用js来做较好!
dt891030 2011-01-04
  • 打赏
  • 举报
回复
必须在Page_Load中写么 我是写在按钮事件中的,textbox是添加到面板中的,当输入字符文本失去焦点时动态添加的textbox就消失了 也不触发事件

事件是这样的
protected void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb != null)
{
Response.Write(tb.ID);
}
else
{
Response.Write("you want to die !");
}
}
koukoujiayi 2011-01-04
  • 打赏
  • 举报
回复
你要在IsPostBack中还要生成一遍!
我粗糙的做了一下,仅仅是验证动态添加的TextBox是可以触发tb_TextChanged事件的!
如:在创建TextBox事件中加一个标志!
.........
.........
plArray.Controls.Add((TextBox)TextArray[i]);
ViewState["flag"] = "1";

在Page_Load中:
if (!IsPostBack)
{
txtLength.Attributes.Add("dir", "rtl");
}
else
{
if (ViewState["flag"] != null)
{
int i = 0;
TextBox textbox = new TextBox();
textbox.ID = "txtNum_" + (i + 1).ToString();
textbox.Width = 35;
textbox.Attributes.Add("dir", "rtl");
textbox.Attributes.Add("onkeypress", "javascript:KeyPress(this);");
textbox.MaxLength = 5;
textbox.AutoPostBack = true;
textbox.TextChanged += new EventHandler(this.tb_TextChanged);
//textbox.Text = "0";
TextArray.Add(textbox);
plArray.Controls.Add((TextBox)TextArray[i]);
tb_TextChanged(null, null); }
}
}
运行的时候输入1,(仅测试1个是否调用tb_TextChanged事件)
产生textBox输入任意字符,离开焦点,触发tb_TextChanged事件!
楼主试试!
ltcszk 2011-01-04
  • 打赏
  • 举报
回复
这种需求为什么不用js做
客户端就能做完的事非得回传给服务器端做,做完了还得再传回来
TimZhuFaith 2011-01-04
  • 打赏
  • 举报
回复
if(!IsPostBack)
{
//code in page_load
}
textbox.TextChanged += new EventHandler(this.tb_TextChanged);


dt891030 2011-01-04
  • 打赏
  • 举报
回复
搞不懂为什么编辑个帖子这么难 下面写文字还看不见
AutoPostBack已经设置为true

源码如下:
protected void btnNew_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtLength.Value.Trim()))
{
plArray.Controls.Clear();
if (int.Parse(txtLength.Value.Trim()) != 0)
{
//plArray.Visible = true;
for (int i = 0; i < int.Parse(txtLength.Value.Trim()); i++)
{
TextBox textbox = new TextBox();
textbox.ID = "txtNum_" + (i + 1).ToString();
textbox.Width = 35;
textbox.Attributes.Add("dir", "rtl");
textbox.Attributes.Add("onkeypress", "javascript:KeyPress(this);");
textbox.MaxLength = 5;
textbox.AutoPostBack = true;
textbox.TextChanged += new EventHandler(this.tb_TextChanged);
textbox.Text = "0";
TextArray.Add(textbox);
plArray.Controls.Add((TextBox)TextArray[i]);
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入数组长度!');", true);

}
}

dt891030 2011-01-04
  • 打赏
  • 举报
回复
测试测试vdf的发生大幅范德萨飞洒地方范德萨飞洒地方dfasdfdsfsad
xrongzhen 2011-01-04
  • 打赏
  • 举报
回复
textbox的autoPostBack设为true

不过不建议这样用,用js处理
boybay 2011-01-04
  • 打赏
  • 举报
回复
autopostback属性设置了吗

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧