2014-10-31 107 views
0

我在我的一個項目中創建動態網格。我在Page_Init方法中添加了網格代碼。通常我會使用按預期工作的綁定列或模板列。 下面是代碼:Sys.WebForms.PageRequestManagerServerErrorException:無效的列名稱:

protected void Page_Init(object source, System.EventArgs e) 
{ 
    if (Session["colnames"] != null) 
    { 
     List<CommanIdTitle> CategoryIdTitle = new List<CommanIdTitle>(); 
     MainSubCategory CateMarks = (MainSubCategory)Session["colnames"]; 
     GridBoundColumn boundColumn_StudentId; 
     boundColumn_StudentId = new GridBoundColumn(); 
     boundColumn_StudentId.DataField = "StudentId"; 
     boundColumn_StudentId.HeaderText = "Student Id"; 
     boundColumn_StudentId.UniqueName = "StudentId"; 
     boundColumn_StudentId.AllowFiltering = false; 
     boundColumn_StudentId.Display = false; 
     Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentId); 

     GridBoundColumn boundColumn_StudentName; 
     boundColumn_StudentName = new GridBoundColumn(); 
     boundColumn_StudentName.DataField = "StudnetName"; 
     boundColumn_StudentName.HeaderText = "Student Name"; 
     boundColumn_StudentName.UniqueName = "StudentName"; 
     boundColumn_StudentName.AllowFiltering = false; 

     Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentName); 

     GridTemplateColumn templateColumn_AverageFinal; 
     templateColumn_AverageFinal = new GridTemplateColumn(); 
     templateColumn_AverageFinal.ItemTemplate = new MyTemplate2("0"); 
     templateColumn_AverageFinal.UniqueName = "Average"; 
     templateColumn_AverageFinal.HeaderText = "Test"; 
     templateColumn_AverageFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
     templateColumn_AverageFinal.AllowFiltering = false; 
     templateColumn_AverageFinal.ReadOnly = true; 
     Grid_CategoryMarks.MasterTableView.Columns.Add(templateColumn_AverageFinal); 
    } 
} 

但現在的新規定是將一些本列。所以我加了GridGroupColumn

這裏是新代碼:

protected void Page_Init(object source, System.EventArgs e) 
{ 
    if (Session["colnames"] != null) 
    { 
     List<CommanIdTitle> CategoryIdTitle = new List<CommanIdTitle>(); 
     MainSubCategory CateMarks = (MainSubCategory)Session["colnames"]; 
     GridBoundColumn boundColumn_StudentId; 
     boundColumn_StudentId = new GridBoundColumn(); 
     boundColumn_StudentId.DataField = "StudentId"; 
     boundColumn_StudentId.HeaderText = "Student Id"; 
     boundColumn_StudentId.UniqueName = "StudentId"; 
     boundColumn_StudentId.AllowFiltering = false; 
     boundColumn_StudentId.Display = false; 
     Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentId); 

     GridBoundColumn boundColumn_StudentName; 
     boundColumn_StudentName = new GridBoundColumn(); 
     boundColumn_StudentName.DataField = "StudnetName"; 
     boundColumn_StudentName.HeaderText = "Student Name"; 
     boundColumn_StudentName.UniqueName = "StudentName"; 
     boundColumn_StudentName.AllowFiltering = false; 
     Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentName); 

     /*This code throws error*/ 
     GridColumnGroup groupHeaderFinal = new GridColumnGroup(); 
     groupHeaderFinal.Name = "TotalAveragePerTerm"; 
     groupHeaderFinal.HeaderText = "Total Average Per Term"; 
     groupHeaderFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
     Grid_CategoryMarks.MasterTableView.ColumnGroups.Add(groupHeaderFinal); 

     GridTemplateColumn templateColumn_AverageFinal; 
     templateColumn_AverageFinal = new GridTemplateColumn(); 
     templateColumn_AverageFinal.ItemTemplate = new MyTemplate2("0"); 
     templateColumn_AverageFinal.UniqueName = "Average"; 
     templateColumn_AverageFinal.HeaderText = ""; 
     templateColumn_AverageFinal.ColumnGroupName = "TotalAveragePerTerm"; 
     templateColumn_AverageFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
     templateColumn_AverageFinal.AllowFiltering = false; 
     templateColumn_AverageFinal.ReadOnly = true; 
     Grid_CategoryMarks.MasterTableView.Columns.Add(templateColumn_AverageFinal); 
    } 
} 

當我加入這個任何回發時發生錯誤

Sys.WebForms.PageRequestManagerServerErrorException:無效的列名:

我找到的一個解決方案是設置網格的EnableViewState = False。但如果我這樣做,viewstate不會維護和網格崩潰,並且所有數據都將丟失。

這是我設計的代碼

<telerik:RadGrid ID="Grid_CategoryMarks" runat="server" EnableViewState="true" 
     AllowFilteringByColumn="True" AllowSorting="True" 
     ShowGroupPanel="false" AutoGenerateColumns="False" PageSize="10" 
     ShowStatusBar="true" OnNeedDataSource="Grid_CategoryMarks_NeedDataSource" OnItemDataBound="Grid_CategoryMarks_ItemDataBound" 
     ShowFooter="True" FilterItemStyle-HorizontalAlign="Left"> 
    <ClientSettings AllowDragToGroup="True"> 
     <Selecting AllowRowSelect="false" UseClientSelectColumnOnly="true"/> 
     <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true"/> 
    </ClientSettings> 
    <MasterTableView AutoGenerateColumns="false" > 
     <NoRecordsTemplate> 
      <asp:Label ID="lbl_rec_msg" runat="server" Text="No record exist"></asp:Label> 
     </NoRecordsTemplate> 
    </MasterTableView> 
</telerik:RadGrid> 

回答

1

我遇到了類似的問題。問題在於你已經在設計中定義了網格,並且在page_init方法中再次創建了該網格。

從設計中刪除它,並嘗試從後面的代碼創建。所以每次創建新的時刻。