简体   繁体   中英

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).

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

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 ".

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?

So that I can include click event of the textbox within the Triggers of the update panel.

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). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.

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 .

You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.

http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM