RadGrid 刷新细节 Table 扩展

RadGrid Refreshes on Detail Table Expansion

我需要在某些列上创建一个包含多个组的网格我的代码是

<telerik:RadGrid Skin="MetroTouch" ID="grdQuestionnaire" RenderMode="Lightweight" runat="server"
                        AllowMultiRowSelection="True" AllowPaging="True" ShowGroupPanel="False"
                        AutoGenerateColumns="False" GridLines="none"
                        showchooser="true"
                        OnItemCommand="grdQuestionnaire_ItemCommand"
                      
                        OnNeedDataSource="grdQuestionnaire_NeedDataSource"
                        OnItemDataBound="grdQuestionnaire_ItemDataBound"
                        OnDetailTableDataBind="grdQuestionnaire_DetailTableDataBind">
                        <GroupingSettings ShowUnGroupButton="false"></GroupingSettings>
                        <MasterTableView GroupLoadMode="Client" NoMasterRecordsText="No Question Added" TableLayout="Fixed" DataKeyNames="QuestionId,QuestionCode,LanguageQ">
                            <DetailTables >
                                <telerik:GridTableView  NoDetailRecordsText="No Options Added" Name="Options" Width="100%">
                                    <Columns>
                                        <telerik:GridBoundColumn SortExpression="OptionDesc" HeaderText="Option" HeaderButtonType="TextButton"
                                            DataField="OptionDesc">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="OptionWeightage" HeaderText="Weightage" HeaderButtonType="TextButton"
                                            DataField="OptionWeightage">
                                        </telerik:GridBoundColumn>
                                    </Columns>
                                </telerik:GridTableView>
                            </DetailTables>
                            <GroupByExpressions>
                                <telerik:GridGroupByExpression>
                                    <SelectFields>
                                        <telerik:GridGroupByField FieldAlias="WorkshopName" FieldName="WorkshopName" HeaderText="Workshop"></telerik:GridGroupByField>
                                    </SelectFields>
                                    <GroupByFields>
                                        <telerik:GridGroupByField FieldName="WorkshopCode"></telerik:GridGroupByField>
                                    </GroupByFields>
                                </telerik:GridGroupByExpression>

                                <telerik:GridGroupByExpression>
                                    <SelectFields>

                                        <telerik:GridGroupByField FieldAlias="" FieldName="AssessmentType" HeaderText="Type"></telerik:GridGroupByField>
                                        <telerik:GridGroupByField FieldAlias="QuestionnaireDesc" FieldName="QuestionnaireDesc" HeaderText="Description" FormatString=""></telerik:GridGroupByField>
                                    </SelectFields>
                                    <GroupByFields>

                                        <telerik:GridGroupByField FieldName="QuestionnaireCode"></telerik:GridGroupByField>
                                    </GroupByFields>
                                </telerik:GridGroupByExpression>

                                <telerik:GridGroupByExpression>
                                    
                                    <SelectFields>
                                        <telerik:GridGroupByField FieldAlias="QuestionCode" FieldName="QuestionCode" HeaderText="Question">                                               
                                        </telerik:GridGroupByField>
                                        
                                    </SelectFields>
                                    <GroupByFields>
                                        <telerik:GridGroupByField FieldName="QuestionCode"></telerik:GridGroupByField>
                                    </GroupByFields>
                                </telerik:GridGroupByExpression>

                            </GroupByExpressions>
                            <Columns>
                                <telerik:GridBoundColumn DataField="QuestionDesc" HeaderText="Question"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="QuestionWeightage" HeaderText="Weightage"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="LanguageQ" HeaderText="Language"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="CreatedBy" HeaderText="Created By"></telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn HeaderText="Action" UniqueName="colAction">
                                    <HeaderStyle HorizontalAlign="Left" Font-Bold="false" VerticalAlign="NotSet" />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="addOption" CommandName="addNewOption" runat="server" CssClass="btn btn-info">Add Options</asp:LinkButton>
                                        <asp:LinkButton ID="deleteQuestion" CommandName="deletequestion" runat="server" CssClass="btn btn-danger">Delete</asp:LinkButton>
                                       
                                    </ItemTemplate>

                                    <HeaderStyle Font-Bold="True" />
                                </telerik:GridTemplateColumn>
                            </Columns>

                        </MasterTableView>
                    </telerik:RadGrid>
  1. 我有一个场景,我需要显示带有 groups 的按钮 - 这部分已完成 通过订阅事件 OnItemDataBound

        protected void grdQuestionnaire_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridGroupHeaderItem)
        {
    
            GridGroupHeaderItem hi = (GridGroupHeaderItem)e.Item;
            DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
            DataRowView drv = ((DataRowView)e.Item.DataItem);
            string columnname = drv.DataView.Table.Columns[0].ColumnName;
    
            switch (columnname)
            {
                case "WorkshopName":
                    {
                        LinkButton btn = new LinkButton();
    
                        btn.Text = "Add workshop";
                        // btn.OnClientClick = "if (!confirm('Are you sure you all information is correct for this employee?')) return false;";
                        btn.CssClass = "btn btn-sm btn-info";
                        btn.Style.Add(HtmlTextWriterStyle.MarginRight, "0px");
                        btn.CommandName = "AddWorkshop";
                        TableCell customcell = new TableCell();
                        customcell.Controls.Add(btn);
                        hi.Cells.Add(customcell);
                        hi.Cells[hi.Cells.Count - 2].ColumnSpan = hi.Cells[hi.Cells.Count - 2].ColumnSpan - 1;
                        break;
                    }
                case "QuestionCode":
                    {
                        LinkButton btn = new LinkButton();
                        btn.Text = "Add question";
                        btn.CssClass = "btn btn-sm btn-success";
                        btn.Style.Add(HtmlTextWriterStyle.MarginRight, "0px");
                        btn.CommandName = "AddQuestion";
                        TableCell customcell = new TableCell();
                        customcell.Controls.Add(btn);
                        hi.Cells.Add(customcell);
                        hi.Cells[hi.Cells.Count - 2].ColumnSpan = hi.Cells[hi.Cells.Count - 2].ColumnSpan - 1;
                        break;
                    }
            }
        }
    }
    

这段代码的输出如下

  1. 扩展时,table 来自其他数据table,我从以下 event OnDetailTableDataBind 添加

        protected void grdQuestionnaire_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        string n = e.DetailTableView.Name;
        string QuestionCode = dataItem.GetDataKeyValue("QuestionCode").ToString();
        string Language = dataItem.GetDataKeyValue("LanguageQ").ToString();
        DataSet ds = clsQuestionnaireDAC.GetOptions(QuestionCode, Language);
        e.DetailTableView.DataSource = ds.Tables[0];
    }
    

我不知道为什么 table 展开时会出现问题,我的 自定义按钮 神秘地消失并使网格静止。这是展开细节后的结果 table 任何人都可以指导我解决这种情况,以防止我的 MasterTableView 像扩展前一样。

尝试使用 ItemCreated 事件。

我怀疑发生了以下情况:

  1. ItemDataBound 触发,您的按钮已创建

  2. 有回发绑定详情table

  3. 此时父行没有反弹,所以ItemDataBound没有触发,所以动态创建的控件没有重新创建,所以消失了

一般来说,在类似的场景中使用模板会更好: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/grouping/group-header-and-footer-templates

例如,如果您有以下定义:

               <GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="ShipCountry" />
                        </GroupByFields>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="ShipCountry" />
                        </SelectFields>
                    </telerik:GridGroupByExpression>
                    <telerik:GridGroupByExpression>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="ShipName" />
                        </GroupByFields>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="ShipName" />
                        </SelectFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>

你可以使用类似这样的东西:

                <GroupHeaderTemplate>
                    <asp:Label runat="server" ID="ShipCountryLabel" Text='<%# "ShipCountry: "+
                    (((GridGroupHeaderItem)Container).AggregatesValues["ShipCountry"]) %>'
                        Visible='<%# ((((GridGroupHeaderItem)Container).AggregatesValues["ShipCountry"]) != null) %>'>
                    </asp:Label>
                    <asp:Label runat="server" ID="ShipNameLabel" Text='<%# "ShipName: "+
                    (((GridGroupHeaderItem)Container).AggregatesValues["ShipName"]) %>'
                        Visible='<%# ((((GridGroupHeaderItem)Container).AggregatesValues["ShipName"]) != null) %>'>
                    </asp:Label>
                    <br />
                    <asp:Button ID="Button2" runat="server" Text="MyButton"
                        Visible='<%# ((((GridGroupHeaderItem)Container).AggregatesValues["ShipName"]) != null) %>' />
                </GroupHeaderTemplate>

这将只显示内部组级别的按钮。

如果您坚持使用代码隐藏方法,则需要使用 ItemCreated。您可以使用下面演示的方法提取字段名称并在页面生命周期的后期添加控件:

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridGroupHeaderItem)
    {
        GridGroupHeaderItem headerItem = e.Item as GridGroupHeaderItem;
        int index = headerItem.GroupIndex.Split('_').Length - 1;
        GridGroupByExpression exp = headerItem.OwnerTableView.GroupByExpressions[index] as GridGroupByExpression;
        string fieldName = exp.GroupByFields[0].FieldName;

        if (fieldName == "ShipName")
        {
            Button button = new Button()
            {
                ID = "GroupButton1",
                Text = "MyButton"
            };

            headerItem.Load += (s, a) =>
            {
                headerItem.DataCell.Controls.Add(new Literal() { Text = headerItem.DataCell.Text });
                headerItem.DataCell.Controls.Add(button);
            };
        }
    }
}

我希望这会有所帮助。