1

I have a web page where I use jQuery AJAX to load data from a database to fill a drop down list. When the jQuery function runs, the server events does not fire.

jQuery:

 $('#Cmb_PDept').on('change', function (e) {
                    e.preventDefault();
                    var DepartmentId = $('#Cmb_PDept :selected').val();
                    if (DepartmentId == 0) {
                        EmpCombo.empty();
                        textbox.val("");

                        return;
                    }
                    $.ajax({
                        type: "POST",
                        cache: false,
                        contentType: "application/json; charset=utf-8",
                        url: '/WebService/GetEmployeeByDepID.asmx/GetEmployee',
                        data: '{ "DepartmentId": "' + DepartmentId + '" }',
                        dataType: 'json',
                        success: function (data) {
                            var data = $.parseJSON(data.d)
                            var options = $("#Cmb_PEmp");
                            options.empty();
                            for (var i = 0; i < data.length ; i++) {
                                options.append("<option value='" + data[i]["EmployeeId"] + "'>" + data[i]["EmployeeName"] + "</option>");   
                            }
                            myEvent();
                        },
                        error: function () { alert("error"); }
                    });
                });

ASP.NET Button control

<asp:Button ID="Btn_PIncrementSave" runat="server" Text="Save" 
            OnClick="Btn_PIncrementSave_Click" CausesValidation="false" />

The onClick event

protected void Btn_PIncrementSave_Click(object sender, EventArgs e)
{
        try
        {
            TxBx_IncrementAmount.Text = Hid_BasicSalary.Value;
        }
        catch (Exception ex)
        {
            Utility.Msg_Error(this.Master, ex.Message);
        }
    }

This event does not fire. I think this is due to

 e.preventDefault();

When I remove this, the server-side event works properly.

6
  • 2
    If you want it to fire why are you even using e.preventdefault? Commented May 6, 2013 at 6:41
  • e.preventDefault(); if removing this working well then what is the issue? Commented May 6, 2013 at 6:42
  • when jquery executs its fuction server side events not fire.i bypass all jquery events then button click fire.when ever i executes jquery events server envents not fire. Commented May 6, 2013 at 6:44
  • you remove the e.PreventDefault() and add ClientIDMode="Static" in dropdown attributes, then try Commented May 6, 2013 at 6:48
  • when i remove e.preventDefult() jquery events not fire Commented May 6, 2013 at 6:48

2 Answers 2

2

your answer is in the title of question, if you use e.PreventDefault() it stops to fire the server side event or if you write return false statement in that case also server side event will not fire. You remove the e.PreventDefault() form your code, it will fire then.

Sign up to request clarification or add additional context in comments.

Comments

1

To your ASP button add this ClientIDMode="Static" and Check. Weather the ASP:Button ID will be changed when the source is moved to Browser. May be that will be one reason. Check the ID by clicking the view source in your Browser.

3 Comments

try the way @RJK is saying, this will also resolve your issue actually when page is rendered asp.net changes the ids on client side so thats the issue
i alredy include cleintMode-sataic in web.confiq file
@AmjadShah Please Read this

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.