1

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
         }
3
  • Do you know the server event isn't fired until the textbox loses focus? Commented Nov 26, 2013 at 5:20
  • yes, event is not fired even after tabbing out of the textbox Commented Nov 26, 2013 at 5:24
  • But if the user is typing in txtFirstName, they are not tabbing out of txtADUserID which is what the server event is attached to. Commented Nov 26, 2013 at 14:00

1 Answer 1

3

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();
      });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I already did exactly the same with an anchor instead of a button. i have required field validators on my page and they were not letting postback happen. So, I added causesvalidation=false to the anchor and everything looks good again. Your answer reaffirmed that the problem is elsewhere other than the jquery code. I ended up using a button with no text. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.