Ken
Ken

Reputation: 744

asp.net textbox changed event not fired when textbox value is changed in jquery code

I have three text boxes, first name txtFirstName, last name txtLastName and AD User ID txtADUserID. Whenever first name or last name is changed, I am assigning AD user id value in Jquery code. I see in browser that ADUserID textbox value is changed.

PROBLEM: I need to check if user id already exist in txtADUserID's ontextchanged server side event. OnTextChanged event is NOT fired at all and postback is also not raised.

ASP.net code

<asp:TextBox ID="txtADUserID" runat="server" MaxLength="255" ClientIDMode="Static" OnTextChanged="txtADUserID_TextChanged" AutoPostBack="true"></asp:TextBox>

Jquery code:

 $(document).ready(function () {
        $("#txtFirstName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
        });
        $("#txtLastName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
        });
    });

Code behind code:

    protected void txtADUserID_TextChanged(object sender, EventArgs e)
            {
// Raise an error if user already exist
         }

Upvotes: 1

Views: 10391

Answers (1)

Adil
Adil

Reputation: 148120

You are changing text through javascript without user interaction that is why you did not get server side postback. You can add a button and bind server side event to that button and perform button click when you need postback. Also store the value of textbox in some hidden field as you will not get the updated textbox value on server side.

$(document).ready(function () {
      $("#txtFirstName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
           $('#buttonId').click();  
      });
      $("#txtLastName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
           $('#buttonId').click();
      });
});

Upvotes: 3

Related Questions