在进行Web应用开发的时候,有时会需要使点击文本框控件(TextBox)执行某个特殊的任务,但TextBox却没有按钮那样的OnClick事件。百度了一段时间,发现了这个解决方法,贴于此,供大家共享。
// .aspx
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="我被隐藏啦" style="display:none" />
// .aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
// 1.x
// TextBox1.Attributes["onclick"] = Page.GetPostBackEventReference(Button1);
// 2.0
TextBox1.Attributes["onclick"] = Client script .GetPostBackEventReference(Button1, null);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(DateTime.Now);
}
在.aspx页面中的<%@ Page …… %>语句中加上属性EnableEventValidation="false"
本文介绍如何在ASP.NET Web应用中,通过设置TextBox控件的onclick属性,使其触发隐藏按钮的点击事件。演示了在.aspx页面中添加属性EnableEventValidation=false来避免验证错误,并提供了.aspx.cs文件中的代码实现步骤。
585

被折叠的 条评论
为什么被折叠?



