Share via


Handling text changed in text box control using Ajax

Question

Friday, September 18, 2009 12:15 PM

 I need to create a web form with a text box control. When someone types text into the textbox I need to post back to a C# or VB method to handle the changed text entered into the text box control.

Is their a specialized Ajax text box control to handle this? Or does someone know of a way to do this using Ajax? I did find an article below that does not use Ajax. This will give you an idea of what I am after.

Ralph

 

http://www.aspxcode.net/free-asp-net-sample-source-code.aspx?Topics=How%20to%20use%20Event%20TextBox%20TextChanged

All replies (3)

Friday, September 18, 2009 11:43 PM ✅Answered

hi, you can use PageMethods or access webservices using javascript

Refer : <CITE>www.singingeels.com/.../Using_Page_Methods_in_ASPNET_AJAX.aspx </CITE>

<CITE></CITE> 


Saturday, September 19, 2009 12:16 PM ✅Answered

Hi

You can attach an onBlur/keypress/keydown event in your client side code and in that event handler you can invoke a PageMethod . The only catch with page method approach is that it has to be declared "static" which can be little irritating sometimes . If some how you get that functionality in a webservice's webmethod then you can also invoke that method through your client side code .

Following is one sample code which explains how you can call webmethods through your client side script . Also I am using jQuery to attach blur event for the textbox :-

<asp:ScriptManager ID="srcx" runat="server">
    <Services>
        <asp:ServiceReference Path="~/TheWebService.asmx" />
    </Services>
</asp:ScriptManager>
<script type="text/javascript" language="javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('.MyText').blur(function() {
            TheWebService.GetTime(onSuccess);
        });
    });

    function onSuccess(result) {
        alert(result);
    }
</script>
<asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="Up1">
    <ContentTemplate>
         <asp:TextBox runat="server" ID="txt" CssClass="MyText"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

Web Service code

public class TheWebService : System.Web.Services.WebService {

    public TheWebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public string GetTime()
    {
        return DateTime.Now.ToString();
    }
}


Saturday, September 19, 2009 12:45 PM ✅Answered

 You can use jQuery to call ASP.Net server side methods

http://www.aspsnippets.com/post/2009/08/27/Check-User-Name-Availability-in-ASPNet-using-JQuery.aspx

I have called it on button click you can call the same on onchange or onkeyup event of the textbox