TextBox OnClick event without Javascript

2019-09-06 14:02发布

问题:

I have a textbox which I want it to be fired on Click. I read some forum in which I wrapped in LinkButton. Here is my mockup UI part:

 <asp:LinkButton runat = "server" OnClick = "txtAgentName_TextChanged"><asp:TextBox ID="txtAgentName" runat="server"></asp:TextBox></asp:LinkButton>

And this is my code behind:

  protected virtual void txtAgentName_TextChanged(object sender, EventArgs e)
    {

    }

And it seems like it is not firing up! I dont want to use Javascript :-P

回答1:

the asp textbox control does not have a server side click event. "OnClick" renders an html "OnClick" attribute for the textbox. Your code will result in a javascript error.

your best bet is to do an ajax call to a WebMethod when the textbox blur event is fired and do something to the textbox based on the result. this article will help you with the webmethod call. using jquery you can handle the blur event pretty easily.