简体   繁体   English

如何避免ASP按钮单击事件的回发

[英]How to avoid postback for the ASP button click event

I am using the jQuery multiselect in that I am using button click event which goes the server side and the populates the values on the other dropdownlist. 我正在使用jQuery multiselect,因为我正在使用按钮单击事件,该事件进入服务器端,并在另一个下拉列表中填充值。 Based on the checked selection in the drop down list it populate the values in the other dropdownlist. 基于下拉列表中选中的选项,它会填充另一个下拉列表中的值。

Issue: 问题:

For eg: in the first jQuery multiselect dropdown if I select 3 values and close the dropdownlist based on these three values checked the details are fetched in the other jQuery multiselect dropdownlist but the last checked value only is checked and that is retaining. 例如:在第一个jQuery multiselect下拉列表中,如果我选择了3个值并基于这三个值关闭了dropdownlist,则在另一个jQuery multiselect下拉列表中获取了详细信息,但仅检查了最后一个选中的值,并保留了该值。 I think it is because of button click event it occurs a asynchronous postback and the values is not retaining. 我认为是由于按钮单击事件,它发生了异步回发,并且值未保留。 Is their any option to prevent that postback in the button click event but the click should be called then only the values will be retained. 他们是否有任何选择可以防止在按钮单击事件中发生回发,但应调用该单击,则仅保留这些值。

For these I am attaching the screen shot: 对于这些,我附上了屏幕截图:

在此处输入图片说明

Based on that three values it is loaded in the other dropdownlist but it is shows "1 selected" that is default "Select". 基于这三个值,它被加载到另一个下拉列表中,但显示为“ 1 selected”,默认为“ Select”。

在此处输入图片说明

But the second dropdownlist I can able to get the values that is checked in the first dropdown list: 但是第二个下拉列表我可以获取第一个下拉列表中检查的值:

在此处输入图片说明

this is my javascript: 这是我的javascript:

  $(document).ready(function () {

            $('.department').multiselect({
                show: ["bounce", 5], hide: ["blind", 1],

                close: function () {
                    debugger;
                    var values = new Array();

                    $(this).multiselect("getChecked").each(function (index, item) {
                        values.push($(item).val());
                    });
                    $("input[id*=selectedValues]").val(values.join(","));
                    document.getElementById("<%=hdnDepartment.ClientID %>").value = values;
                    if (document.getElementById("<%=hdnDepartment.ClientID %>").value != "") {
                        **$("#<%=Button1.ClientID %>")[0].click();**

                    }
                }

            })
        .multiselectfilter();
        });

protected void Button1_Click(object sender, EventArgs e)
    {
        Populate();
    }

Correct me if i am wrong... 如果我错了请纠正我...

You can use OnClientClick and return false or true as per your requirement. 您可以使用OnClientClick并根据需要返回false或true。 When OnClientClick return true postback will occur else no postback 当OnClientClick返回true时将发生回发,否则不会回发

 <script type="text/javascript">
      function pageLoad() {
           $("#<%= DepartmentsListBox.ClientID %>").multiselect({
                create: function () {
                     $(this).data("initValues", $.map($("option:selected", this), function (option) { return option.value; }).join(","));
                },
                close: function () {
                     var selectedValues = $.map($("option:selected", this), function (option) { return option.value; }).join(",");
                     if (selectedValues !== $(this).data("initValues")) {
                          __doPostBack("<%= DepartmentsListBox.UniqueID %>", "");
                     }
                }
           });

           $("#<%= EmployeesListBox.ClientID %>").multiselect();
      }
 </script>


 <asp:UpdatePanel runat="server" UpdateMode="Always">
      <ContentTemplate>
           <asp:ListBox runat="server" ID="DepartmentsListBox" DataValueField="DepartmentId"
                SelectionMode="Multiple" DataTextField="Name" Width="300px"></asp:ListBox>
           <br />
           <asp:ListBox runat="server" ID="EmployeesListBox" Width="300px" SelectionMode="Multiple" AppendDataBoundItems="true"
                DataValueField="EmployeeId" DataTextField="Name"></asp:ListBox>
           <br />
           <asp:Button runat="server" ID="SubmitButton" Text="Submit" />
      </ContentTemplate>
 </asp:UpdatePanel>

Server code 服务器代码

private static Random rnd = new Random();

private string[] selectedDepartments;
private string[] SelectedDepartments
{
    get
    {
        return selectedDepartments ?? (selectedDepartments = DepartmentsListBox.Items.OfType<ListItem>().Where(item => item.Selected).Select(item => item.Value).ToArray());
    }
}

protected void Page_Init(object sender, EventArgs e)
{
    EmployeesListBox.PreRender += new EventHandler(EmployeesListBox_PreRender);
    DepartmentsListBox.SelectedIndexChanged += new EventHandler(DepartmentsListBox_SelectedIndexChanged);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDepartments();
    }
}

void EmployeesListBox_PreRender(object sender, EventArgs e)
{
    if (EmployeesListBox.Items.Count > 0)
    {
        EmployeesListBox.Attributes.Remove("disabled");
    }
    else
    {
        EmployeesListBox.Attributes["disabled"] = "disabled";
    }
}

private void BindDepartments()
{
    var departments = Enumerable.Range(1, 10)
        .Select(id => new { DepartmentId = id, Name = "Department " + id.ToString() });
    DepartmentsListBox.DataSource = departments;
    DepartmentsListBox.DataBind();
}

void DepartmentsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    var employees = Enumerable.Range(1, 10)
        .Select(id => new { EmployeeId = id, DepartmentId = rnd.Next(1, 10), Name = "Employee #" + id.ToString() })
        .Where(emp => SelectedDepartments.Contains(emp.DepartmentId.ToString())).ToList();

    for (int itemIndex = EmployeesListBox.Items.Count - 1; itemIndex >= 0; itemIndex--)
    {
        var employee = employees.FirstOrDefault(emp => emp.EmployeeId.ToString() == EmployeesListBox.Items[itemIndex].Value);
        if (employee == null)
        {
            EmployeesListBox.Items.RemoveAt(itemIndex);
        }
        else
        {
            employees.Remove(employee);
        }
    }

    EmployeesListBox.DataSource = employees;
    EmployeesListBox.DataBind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM