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.

4

2 回答 2

2

您的答案在问题的标题中,如果您使用e.PreventDefault()它停止触发服务器端事件,或者如果您return false在这种情况下编写语句,服务器端事件也不会触发。您删除e.PreventDefault()代码的表单,然后它将触发。

于 2013-05-06T06:42:12.823 回答
1

向您的 ASP 按钮添加此项ClientIDMode="Static"并检查。当源移动到浏览器时,ASP:Button ID将发生变化。可能这将是一个原因。ID通过单击浏览器中的查看源来检查。

于 2013-05-06T06:43:47.757 回答