[英]Creating an event for a textbox and AJAX Update Panel Control
I wanted to create a "Click" event for a textbox in C# (as there isn't any). 我想为C#中的文本框创建“点击”事件(因为没有)。
So, this way 所以这样
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page. 然后,我想加载图像而不重新加载页面。
So I used the AJAX UpdatePanel Control and this worked fine with 因此,我使用了AJAX UpdatePanel控件,并且在
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as a real event or something I couldn't figure out. 但是,与我创建的事件无关,因为编译器无法将我的新事件识别为真实事件或无法确定的事件。
I added the button1_click event according to Step 8 of " Refreshing an UpdatePanel Control with an External Button ". 我根据“ 使用外部按钮刷新UpdatePanel控件 ”的步骤8添加了button1_click事件。
The click event of textbox is not shown in this option: 文本框的单击事件未显示在此选项中:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox
class or, to make this event visible within the above option? 所以我的问题是,有什么办法可以在
System.Web.UI.WebControls.TextBox
类中添加此事件,还是可以在上述选项中使此事件可见?
So that I can include click event of the textbox within the Triggers of the update panel. 这样我就可以在更新面板的“触发器”中包含文本框的click事件。
If you try to create a Click event for a TextBox
, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). 如果您尝试为
TextBox
创建Click事件,则每次用户单击您的文本框时,都会触发回发到服务器(甚至评估是否需要在处理事件时做一些事情)。 This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic. 这是非常低效的-您应该使用JavaScript处理浏览器中的点击,然后使用客户端逻辑触发UpdatePanel。
This lets you trigger a call to the server if you need it but avoid it when you don't. 这使您可以在需要时触发对服务器的调用,但在不需要时避免调用。 If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the
TextBox
. 如果您具有服务器端事件处理程序,则每次用户单击
TextBox
时,您的代码都会回发到服务器(重新加载页面)。
You can read this link (and others) about using __doPostBack()
on the client side to trigger an UpdatePanel
to perform a postback. 您可以阅读有关在客户端使用
__doPostBack()
触发UpdatePanel
执行回发的链接(及其他链接)。
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/ http://encosia.com/easily-refresh-an-updatepanel-using-javascript/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.