在Jquery UI Multiselect小部件中回发后,所选项目仍保持选中状态

时间:2014-02-27 11:05:01

标签: c# javascript jquery asp.net

我正在尝试在asp.net中创建一个GridView,它可以像Excel一样过滤gridview的每一列。 为此我创建了一个Gridview,并在GridView的每一列的标题上使用Jquery multiselect小部件来进行过滤。

<cc1:GridView ID="gvDailyTracker" runat="server" CssClass="mGrid" CellSpacing="2" AutoGenerateColumns="false"
              ShowFooter="false" AllowPaging="true" OnPageIndexChanging="gvDailyTracker_PageIndexChanging" OnRowDataBound="gvDailyTracker_RowDataBound"
              OnRowCommand="gvDailyTracker_RowCommand" OnRowEditing="gvDailyTracker_RowEditing" OnSelectedIndexChanged="gvDailyTracker_SelectedIndexChanged"
              OnDataBound="gvDailyTracker_DataBound">
    <EmptyDataRowStyle BackColor="LightBlue" Width="1180" />
    <EmptyDataTemplate>
        No Data Found From The Current Selection,Please Select  Appropriate Search Criteria.  
    </EmptyDataTemplate>
    <Columns>
        <asp:TemplateField SortExpression="DateProcessed" HeaderStyle-Font-Bold="true"
            HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
            <HeaderTemplate>
                <select id="ddlDateProcessed" name="test" runat="server" multiple="true" style="width: 100px;">
                </select>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblDateProcessed" runat="server" Text='<%#Eval("DateProcessed") %>' CssClass="GridContent" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField SortExpression="InvoiceFileName" HeaderStyle-Font-Bold="true"
            HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
            <HeaderTemplate>
                <select id="ddlInvoiceFilename" name="test" runat="server" multiple="true" style="width: 100px;">
                </select>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblInvoiceFileName" runat="server" Text='<%#Eval("InvoiceFileName") %>' CssClass="GridContent" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</cc1:GridView>
<script type="text/javascript">        
    $(document).ready(function () {
        $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect({
            SelectedText: 'DateProcessed',
            noneSelectedText: 'DateProcessed',
            close: function () {
                var dateProcessed = $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                document.getElementById('<%=txtddlDateProcessed.ClientID%>').value = dateProcessed;
                document.getElementById('<%=btnPostBack.ClientID %>').click();
            },
            beforeopen: function () {
                $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect("checkAll");
            },
            minWidth: 120
        });


        $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect({
            SelectedText: 'InvoiceFileName',
            noneSelectedText: 'InvoiceFileName',
            close: function () {
                var InvoiceFilename = $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                document.getElementById('<%=txtddlInvoiceFileName.ClientID%>').value = InvoiceFilename;
                document.getElementById('<%=btnPostBack.ClientID %>').click();
            },
            beforeopen: function () {
                $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect("checkAll");
            },
            minWidth: 120
        });
    });
    function test() {
                BindAllMultiSelectData();
                var dateProcessed = $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                var InvoiceFilename = $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                document.getElementById('<%=txtddlDateProcessed.ClientID%>').value = dateProcessed;
                document.getElementById('<%=txtddlInvoiceFileName.ClientID%>').value = InvoiceFilename;
                }
    function BindAllMultiSelectData() {
        $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect({
            SelectedText: 'DateProcessed',
            noneSelectedText: 'DateProcessed',
            close: function () {
                var dateProcessed = $("#MainContent_gvDailyTracker_ddlDateProcessed").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                document.getElementById('<%=txtddlDateProcessed.ClientID%>').value = dateProcessed;
                document.getElementById('<%=btnPostBack.ClientID %>').click();
            },
           minWidth: 120
       });


        $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect({
            SelectedText: 'InvoiceFileName',
            noneSelectedText: 'InvoiceFileName',
            close: function () {
                var InvoiceFilename = $("#MainContent_gvDailyTracker_ddlInvoiceFilename").multiselect("getChecked").map(function () {
                    return this.value;
                }).get();
                document.getElementById('<%=txtddlInvoiceFileName.ClientID%>').value = InvoiceFilename;
                document.getElementById('<%=btnPostBack.ClientID %>').click();
            },
            minWidth: 120
        });
</script>

用于过滤gridView的C#代码

protected void btnPostBack_Click(object sender, EventArgs e)
{
    if (!txtddlDateProcessed.Text.Equals(null) && txtddlDateProcessed.Text != string.Empty)
    {
    string filter=txtddlDateProcessed.Text;
    filter = filter.Replace(",", "','");
    string strFilter="[DateProcessed]" + " in ('" + filter + "')";
    FilterHeader(strFilter);
    }

    if (!txtddlInvoiceFileName.Text.Equals(null) && txtddlInvoiceFileName.Text != string.Empty)
    {
    string filter=txtddlInvoiceFileName.Text;
    filter = filter.Replace(",", "','");
    string strFilter="[InvoiceFileName]" + " in ('" + filter + "')";
    FilterHeader(strFilter);
    }
     ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "test();", true);
}

到目前为止,每件事情都运转正常,我可以过滤网格视图。 现在我真正想要的是,在过滤网格视图后,在Multi Select小部件上进行的选择仍然是静止的。目前在过滤后,所有多选项都未经检查。

注意:-1。我动态地绑定Multiselect。(该列值的不同记录绑定到多选)

private void BindHeaderDropDown(string columnName, DataTable dt, string controlName)
{
    System.Web.UI.HtmlControls.HtmlSelect objControlName = (System.Web.UI.HtmlControls.HtmlSelect)gvDailyTracker.HeaderRow.FindControl(controlName);
    DataView view=new DataView(dt);
    DataTable distinctValues = view.ToTable(true, columnName);
    objControlName.DataSource = distinctValues;
    objControlName.DataTextField = columnName;
    objControlName.DataValueField = columnName;
    objControlName.DataBind();
}

在绑定网格后的页面加载中,我通过调用BindHeaderDropDown函数绑定Multiselect小部件。在使用所选值过滤网格视图后,我再次调用相同的函数,即BindHeaderDropDown绑定Multiselect。如果我在过滤后没有调用此函数,则没有任何内容绑定到Multiselect,如果没有单个复选框项,则为空。

请帮助我并指导我,我会做什么? 在此先感谢。

2 个答案:

答案 0 :(得分:0)

当您重新绑定Multiselect控件时,在每个回发(页面加载方法)中,这就是用户选择的值丢失的原因,因为您正在重新生成过滤器控件。

要保留用户选择,您可以更新重新绑定方法并添加逻辑以检查它是初始加载还是回发。如果是回发后再重新绑定控件,请在相应的过滤器控件中手动添加/设置值。

希望它可以解决您的问题。谢谢

private void BindHeaderDropDown(string columnName, DataTable dt, string controlName)
{
  System.Web.UI.HtmlControls.HtmlSelect objControlName = (System.Web.UI.HtmlControls.HtmlSelect)gvDailyTracker.HeaderRow.FindControl(controlName);
  DataView view=new DataView(dt);
  DataTable distinctValues = view.ToTable(true, columnName);
  objControlName.DataSource = distinctValues;
  objControlName.DataTextField = columnName;
  objControlName.DataValueField = columnName;
  objControlName.DataBind();
  // check if it is post-back
  objControlName.Text = Latest User value;
}

答案 1 :(得分:0)

更好的是你可以在隐藏字段中保存多选值。再次根据隐藏字段值,你可以在多选下拉列表中填充这些项目。

此方法用于将值存储在隐藏字段中

close: function (event, ui) {

                var values = new Array();
                $("#ddlAssignedTo").multiselect("getChecked").each(function (index, item) {
                    values.push($(item).val());
                });
                $("input[id*=hdnAssignTo]").val(values.join(","));
            }

这是根据hiddenfield

填充multislect下拉列表中的值的方法
  function loadSelectedUsers() {
        $("#ddlAssignedTo").multiselect("uncheckAll");

        var listAssigned = $("#ctl00_MainContent_ddlAssignedTo option:selected").length;

        if (listAssigned == 0) {

            if ($("#<%=hdnAssignTo.ClientID%>").val() != null && $("#<%=hdnAssignTo.ClientID%>").val() != undefined && $("#<%=hdnAssignTo.ClientID%>").val() != "") {
                var arrUserIdSelected = $("#<%=hdnAssignTo.ClientID%>").val().split(',');


                if (arrUserIdSelected != null && arrUserIdSelected.length > 0) {
                    for (var iCount = 0; iCount < arrUserIdSelected.length; iCount++) {
                        $("#ddlAssignedTo").multiselect("widget").find("input:checkbox[value='" + arrUserIdSelected[iCount] + "']").attr("checked", "checked");
                        $("#ddlAssignedTo option[value='" + arrUserIdSelected[iCount] + "']").attr("selected", 1);
                        $("#ddlAssignedTo").multiselect("refresh");
                    }
                }
            }
        } 
    }