嵌套Telerik RadGrid的NeedDataSource在父RadGrid上进行分页后触发

时间:2013-08-28 21:41:39

标签: c# asp.net .net telerik telerik-grid

我正在使用嵌套的Telerik RadGrid,只有在扩展父Rad Grid中的特定行时才应填充它。这适用于父网格的第一页,并且只触发父的NeedDataSource。此外,子嵌套网格的NeedDataSource只在扩展行时正确触发。

但是,当我将父网格分页到第二页时,子RadGrid的NeedDataSource将为父RadGrid的每一行触发,即使父网格中没有任何行被展开。

我该怎么做才能解决这个问题?我下面的代码中的TelerikRadGrid1是父RadGrid,而TelerikRadGrid2是嵌套的子RadGrid。

.aspx页面,文件代码背后的代码如下:

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="IgnoreButton">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="UnMatchedClientDBCustomers_TelerikRadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="MatchButton">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="UnMatchedClientDBCustomers_TelerikRadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="UnMatchedClientDBCustomers_TelerikRadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="UnMatchedClientDBCustomers_TelerikRadGrid1" />
                    <telerik:AjaxUpdatedControl ControlID="PotentialMasterListDBMatches_TelerikRadGrid2" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    <telerik:RadGrid ID="runat" UnMatchedClientDBCustomers_TelerikRadGrid1="server" AllowPaging="true" 
        AllowSorting="true" AllowFilteringByColumn="True" AutoGenerateColumns="false"
        onneeddatasource="UnMatchedClientDBCustomers_TelerikRadGrid1_NeedDataSource" 
        onitemcommand="UnMatchedClientDBCustomers_TelerikRadGrid1_ItemCommand" Width="80%" Style="min-height: 360px;">
         <ClientSettings>
             <Scrolling AllowScroll="false"/>
         </ClientSettings>
        <MasterTableView DataKeyNames="CustomerID, MemberID" TableLayout="Auto">

            <ColumnGroups>
                <telerik:GridColumnGroup Name="Name" HeaderText="Name"
                    HeaderStyle-HorizontalAlign="Center" >
                </telerik:GridColumnGroup>
                <telerik:GridColumnGroup Name="Emails" HeaderText="Emails"
                    HeaderStyle-HorizontalAlign="Center" >
                </telerik:GridColumnGroup>
                <telerik:GridColumnGroup Name="Match" HeaderText="Match Criteria"
                    HeaderStyle-HorizontalAlign="Center" >
                </telerik:GridColumnGroup>
                <telerik:GridColumnGroup Name="Address" HeaderText="Address"
                    HeaderStyle-HorizontalAlign="Center">
                </telerik:GridColumnGroup>
                <telerik:GridColumnGroup Name="Action" HeaderText="Action"
                    HeaderStyle-HorizontalAlign="Center" >
                </telerik:GridColumnGroup>
            </ColumnGroups>

            <CommandItemSettings ExportToPdfText="Export to PDF" ShowExportToExcelButton="true"></CommandItemSettings>

            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>

            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
            <Columns>
                <telerik:GridTemplateColumn HeaderText="Ignore" ColumnGroupName="Action" AllowFiltering="false"><HeaderStyle Width="50px" />
                    <ItemTemplate>
                        <asp:LinkButton ID="IgnoreButton" runat="server" Text="Ignore" OnClientClick="IgnoreConfirm()" CommandName="IgnoreButtonClick" CommandArgument='<%#Eval("CustomerID")%>' />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID" Visible="false"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MemberID" HeaderText="Member Number" UniqueName="MemberNumber" ColumnGroupName="Name" FilterControlWidth="50px"><HeaderStyle Width="50px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" UniqueName="LastName" ColumnGroupName="Name"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" UniqueName="First" ColumnGroupName="Name"><HeaderStyle Width="50px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email" ColumnGroupName="Emails" FilterControlWidth="130px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="LoginID" HeaderText="Login ID" UniqueName="LoginID" ColumnGroupName="Emails" FilterControlWidth="130px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MemberNoMatch" HeaderText="Member Number Match" UniqueName="MemberNoMatch" ColumnGroupName="Match"><HeaderStyle Width="50px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="LastNameMatch" HeaderText="Last Name Match" UniqueName="LastNameMatch" ColumnGroupName="Match" FilterControlWidth="130px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="EmailMatch" HeaderText="Email Match" UniqueName="EmailMatch" ColumnGroupName="Match" FilterControlWidth="130px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Company" HeaderText="Company" UniqueName="Company" ColumnGroupName="Address"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Address" HeaderText="Address" UniqueName="Address" ColumnGroupName="Address"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="City" HeaderText="City" UniqueName="City" ColumnGroupName="Address" FilterControlWidth="50px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="State" HeaderText="State" UniqueName="State" ColumnGroupName="Address" FilterControlWidth="30px"><HeaderStyle Width="20px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PostalCode" HeaderText="Postal Code" UniqueName="PostalCode" ColumnGroupName="Address" FilterControlWidth="50px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone Number" UniqueName="Phone" ColumnGroupName="Address" FilterControlWidth="50px"><HeaderStyle Width="100px" /><ItemStyle Wrap="true" /></telerik:GridBoundColumn>
            </Columns>
            <NestedViewTemplate>
                <div>
                    <telerik:RadGrid ID="PotentialMasterListDBMatches_TelerikRadGrid2" runat="server" OnNeedDataSource="PotentialMasterListDBMatches_TelerikRadGrid2_NeedDataSource" AutoGenerateColumns="false">
                        <MasterTableView>
                            <Columns>
                                <telerik:GridTemplateColumn HeaderText="Update" ColumnGroupName="Action" AllowFiltering="false"><HeaderStyle Width="40px" />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="MatchButton" runat="server" Text="Match" OnClientClick="MatchConfirm()" OnClick="MatchButton_Click" />
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn DataField="ClientDBCustomerID" HeaderText="CustomerID" UniqueName="ClientDBCustomerID" Display="false"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="MemberID" HeaderText="Member Number" UniqueName="MasterListDBMemberNumber" ColumnGroupName="Name"><HeaderStyle Width="50px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" UniqueName="Last" ColumnGroupName="Name"><HeaderStyle Width="100px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="FullName" HeaderText="Full Name" UniqueName="Full" ColumnGroupName="Name"><HeaderStyle Width="200px" /><HeaderStyle Width="100px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="AdmitDate" HeaderText="Admit Date" UniqueName="AdmitDate" ColumnGroupName="Name"><HeaderStyle Width="25px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Status" HeaderText="Status" UniqueName="Status" ColumnGroupName="Name"><HeaderStyle Width="15px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="PrivateEmail" HeaderText="Private Email" UniqueName="PrivateEmail" ColumnGroupName="Emails"><HeaderStyle Width="100px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="PublicEmail" HeaderText="Public Email" UniqueName="PublicEmail" ColumnGroupName="Emails"><HeaderStyle Width="100px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="FirmName" HeaderText="Firm Name" UniqueName="FirmName" ColumnGroupName="Address"><HeaderStyle Width="100px" /></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Address" HeaderText="Address" UniqueName="Address" ColumnGroupName="Address"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="City" HeaderText="City" UniqueName="City" ColumnGroupName="Address"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="State" HeaderText="State" UniqueName="State" ColumnGroupName="Address"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Country" HeaderText="Country" UniqueName="Country" ColumnGroupName="Address"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ZipCode" HeaderText="Zip Code" UniqueName="ZipCode" ColumnGroupName="Address"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone" UniqueName="Phone" ColumnGroupName="Address"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
                    <%--<asp:LinkButton ID="CustomMatch" runat="server" Text="Custom Match"></asp:LinkButton>--%>

                </div>
            </NestedViewTemplate>

            <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
            </EditFormSettings>

            <PagerStyle PageSizes="5,10" PagerTextFormat="{4}Currently we have <strong>{5}</strong> members matching your search criteria"
                PageSizeLabelText="Members per page:" />
        </MasterTableView>

        <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>

        <FilterMenu EnableImageSprites="False"></FilterMenu>
        <ExportSettings SuppressColumnDataFormatStrings="false" IgnorePaging="true" HideStructureColumns="true" OpenInNewWindow="true" ExportOnlyData="true">
            <Excel Format="Biff"></Excel>
        </ExportSettings>
    </telerik:RadGrid>







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

        }
    }

    protected void UnMatchedClientDBCustomers_TelerikRadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        List<ClientDBUnmatchedDetail> ClientDBUnmatchedDetails_UnMatchedCustomers_BindToTelerikRadGrid;

        using (ClientDBEntities ClientDBContext = new ClientDBEntities())
        {
            ClientDBUnmatchedDetails_UnMatchedCustomers_BindToTelerikRadGrid = ClientDBContext.ClientDBUnmatchedDetails.ToList();
        }
        UnMatchedClientDBCustomers_TelerikRadGrid1.DataSource = ClientDBUnmatchedDetails_UnMatchedCustomers_BindToTelerikRadGrid;
    }

    protected void PotentialMasterListDBMatches_TelerikRadGrid2_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {

        GridDataItem parentItem = ((sender as RadGrid).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;

        var MemberID = parentItem.GetDataKeyValue("MemberID").ToString();
        string CustomerID = parentItem.GetDataKeyValue("CustomerID").ToString();

        List<UnMatchedMasterListDBMembers> MasterListDBUnMatchedDetails_PotentialMasterListDBMatches_BindToTelerikRadGrid = null;

        // If there is match between member numbers
        if (parentItem["MemberNoMatch"].Text == bool.TrueString)
        {
            decimal dMemberID = Convert.ToDecimal(MemberID);

            using (appsEntities appsEntitiescontext = new appsEntities())
            {
                MasterListDBUnMatchedDetails_PotentialMasterListDBMatches_BindToTelerikRadGrid = appsEntitiescontext.membership_all
                    .Where(x => x.MemNumber == dMemberID)
                    .Select(y => new UnMatchedMasterListDBMembers()
                    {
                        ClientDBCustomerID = CustomerID,
                        MemberDetails = y
                    }).ToList();
            }

        }

        // If there is match between email addresses
        else if (parentItem["EmailMatch"].Text == bool.TrueString)
        {
            string email = (parentItem["Email"].Text).Trim().ToLower().ToString();
            using (var appsEntitiescontext = new appsEntities())
            {
                MasterListDBUnMatchedDetails_PotentialMasterListDBMatches_BindToTelerikRadGrid = appsEntitiescontext.membership_all
                    .Where(x => x.PrivateEmail.Trim().ToLower().ToString() == email || x.PublicEmail.Trim().ToLower().ToString() == email)
                    .Select(y => new UnMatchedMasterListDBMembers()
                    {
                        ClientDBCustomerID = CustomerID,
                        MemberDetails = y
                    }).ToList();
            }
        }

        // If there is match between Last Names, then get the corresponding MasterDB members 
        else if (parentItem["LastNameMatch"].Text == bool.TrueString)
        {
            string lastName = (parentItem["LastName"].Text).Trim().ToLower().ToString();

            using (var context = new appsEntities())
            {
                MasterListDBUnMatchedDetails_PotentialMasterListDBMatches_BindToTelerikRadGrid = context.membership_all
                    .Where(x => x.LastName.Trim().ToLower() == lastName)
                    .Select(y => new UnMatchedMasterListDBMembers()
                    {
                        ClientDBCustomerID = CustomerID,
                        MemberDetails = y
                    }).ToList();
            }
        }

        (sender as RadGrid).DataSource = MasterListDBUnMatchedDetails_PotentialMasterListDBMatches_BindToTelerikRadGrid;
    }

    protected void UnMatchedClientDBCustomers_TelerikRadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "ExpandCollapse" && e.Item is GridDataItem)
        {
            GridDataItem parentItem_ClientDBCustomer_TelerikRADGrid1Row = e.Item as GridDataItem;

            RadGrid childItem_potentialMasterListDBMemberMatches_TelerikRADGrid2 = parentItem_ClientDBCustomer_TelerikRADGrid1Row.ChildItem.FindControl("PotentialMasterListDBMatches_TelerikRadGrid2") as RadGrid;
            childItem_potentialMasterListDBMemberMatches_TelerikRADGrid2.Rebind();
        }
        else if (e.CommandName == "IgnoreButtonClick" && e.Item is GridDataItem)
        {
            string confirmValue = Request.Form["ConfirmIgnore"];

            if (confirmValue == "Yes")
            {
                GridDataItem item = e.Item as GridDataItem;
                var MemberID = item.GetDataKeyValue("MemberID").ToString();
                var CustomerID = item.GetDataKeyValue("CustomerID").ToString();

                try
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        using (ClientDBEntities ClientDBContext = new ClientDBEntities())
                        {
                            // Find the ClientDB customer who's being ignored in the ClientDBUnmatchedDetails db table
                            ClientDBUnmatchedDetail ClientDBUnMatchedDetails_CustomerToBeIgnored = ClientDBContext.ClientDBUnmatchedDetails.Where(x => x.CustomerID == CustomerID).FirstOrDefault();

                            //check if customer is already in the ClientDBIgnore table
                            bool CustomerAlreadyExistsIn_ClientDBIgnore = false;
                            CustomerAlreadyExistsIn_ClientDBIgnore = ClientDBContext.ClientDBIgnore.Any(x => x.CustomerID == CustomerID);

                            if (!CustomerAlreadyExistsIn_ClientDBIgnore)
                            {
                                //add the Ignored ClientDB Customer to the ClientDBIgnore db table
                                ClientDBContext.ClientDBIgnore.AddObject(
                                    new ClientDBIgnore()
                                    {
                                        //<class properties are set here>
                                    }
                                 );
                            }
                            // No need to do anything to the associated courses/credits information because they will already be in the ClientDBUnMatchedCreditDetails db table

                            //delete the Ignored ClientDB Customer from the ClientDBUnmatchedDetails db table
                            ClientDBContext.ClientDBUnmatchedDetails.DeleteObject(ClientDBUnMatchedDetails_CustomerToBeIgnored);

                            // Save changes
                            ClientDBContext.SaveChanges();
                            //scope.Complete();
                        }
                        scope.Complete();
                    }
                    UnMatchedClientDBCustomers_TelerikRadGrid1.Rebind();

                    // If Transaction successfull and complete, notify the user 
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('ClientDB Customer Ignored')", true);
                }
                catch (Exception Ex)
                {
                    // In case of failure, notify the user
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Error Ignoring the ClientDB Customer. Try doin it again or please call IT HelpDesk')", true);
                }
            }
        }

    }

    public void MatchButton_Click(object sender, EventArgs e)
    {
        string confirmValue = Request.Form["ConfirmMatch"];
        if (confirmValue == "Yes")
        {
            GridDataItem ClientDBCustomer_TelerikRADGrid1Row = ((LinkButton)sender).Parent.Parent as GridDataItem;
            var MemberID = ClientDBCustomer_TelerikRADGrid1Row["MasterListDBMemberNumber"].Text;
            var CustomerID = ClientDBCustomer_TelerikRADGrid1Row["ClientDBCustomerID"].Text;

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (ClientDBEntities ClientDBContext = new ClientDBEntities())
                    {
                        //find the manually matched ClientDB customer in the ClientDBUnmatchedDetails table
                        ClientDBUnmatchedDetail deleteClientDBUnMatchedMemberObject = ClientDBContext.ClientDBUnmatchedDetails.Where(x => x.CustomerID == CustomerID).Single();

                        // check if customer is already in the ClientDBRules tables
                        bool CustomerAlreadyExistsIn_ClientDBRules = false;
                        CustomerAlreadyExistsIn_ClientDBRules = ClientDBContext.ClientDBRules.Any(x => x.CustomerID == CustomerID);

                        if (!CustomerAlreadyExistsIn_ClientDBRules)
                        {
                            //add the manually matched ClientDB Customer to the ClientDBRules db table
                            ClientDBContext.ClientDBRules.AddObject(
                                new ClientDBRule()
                                {
                                    //<class properties are set here>
                                }
                             );
                        }

                        //find all matching courses/credits (and which will be removed) from the ClientDBUnMatchedCreditDetails table for this ClientDB customer
                        IQueryable<ClientDBUnMatchedCreditDetails> deleteClientDBUnmatchedCreditDetailsObjects = ClientDBContext.ClientDBUnMatchedCreditDetails.Where(x => x.CustomerID == CustomerID);

                        //for each matching course in the ClientDBUnMatchedCreditDetails table
                        foreach (ClientDBUnMatchedCreditDetails deleteClientDBUnmatchedCreditDetailsObject in deleteClientDBUnmatchedCreditDetailsObjects)
                        {
                            //if course does not exist in ClientDBCreditDetails table, add that course to ClientDBCreditDetails
                            bool courseAlreadyExistsInClientDBCreditDetails = false;
                            //courseAlreadyExistsInClientDBCreditDetails = ClientDBContext.ClientDBCreditDetails.Where(x => x.RegistrationID == deleteClientDBUnmatchedCreditDetailsObject.RegistrationID).SingleOrDefault() != null;
                            courseAlreadyExistsInClientDBCreditDetails = ClientDBContext.ClientDBCreditDetails.Any(x => x.RegistrationID == deleteClientDBUnmatchedCreditDetailsObject.RegistrationID);

                            if (!courseAlreadyExistsInClientDBCreditDetails)
                            {
                                //add course to ClientDBCreditDetails
                                ClientDBContext.ClientDBCreditDetails.AddObject(
                                    new ClientDBCreditDetail()
                                    {
                                        //<class properties are set here>
                                    }
                                );
                            }
                            // else
                            // do nothing

                            // and then delete that course from the ClientDBUnMatchedCreditDetails table
                            ClientDBContext.ClientDBUnMatchedCreditDetails.DeleteObject(deleteClientDBUnmatchedCreditDetailsObject);
                        }

                        //delete the manually matched ClientDB Customer from ClientDBUnmatchedDetails db table
                        ClientDBContext.ClientDBUnmatchedDetails.DeleteObject(deleteClientDBUnMatchedMemberObject);

                        //save changes to the db
                        ClientDBContext.SaveChanges();
                        //scope.Complete();
                    }
                    scope.Complete();
                }

                UnMatchedClientDBCustomers_TelerikRadGrid1.Rebind();

                // If Transaction successfull and complete, notify the user 
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Match Successful')", true);

            }
            catch (Exception Ex)
            {
                // In case of failure, notify the user
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Error Executing the match rule. Try doing it again or please call IT HelpDesk')", true);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

你永远不应该在NeedDataSource事件中调用.Rebind()或.DataBind();我猜这至少会导致你的一些问题。

http://www.telerik.com/help/aspnet-ajax/grid-advanced-data-binding.html