2016-07-14 54 views
0

我有一个从后面的代码填充的gridview,并有大约300行。当我尝试访问包含它的页面时,一切都会加载,然后大约5秒钟后,程序退出并显示以下错误消息: enter image description here 如果按下继续,应用程序将停止运行。但是,当我查看页面时,所有数据都已加载到gridview中(但当然,由于会话已停止,我的链接等无法工作)。加载Gridview

如果我在填充gridview的表中填入较少的数据,我不会收到错误(它可以处理大约30行 - 我不确定数据太多的确切点)。无论如何,由于它是完全相同的代码,但数据量少,我知道我实际上并没有像消息所暗示的那样有无限循环或无限递归。

这里是GridView控件的HTML:

<div id="dvGrid" class="gridTable"> 
    <asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound"> 
    <Columns> 
     <asp:BoundField DataField="Edit" HtmlEncode="false" HeaderText="" HeaderStyle-Wrap="false" SortExpression="Edit" /> 
    </Columns> 
    </asp:GridView> 
</div> 

这里是它背后的代码填充(这是在Page_Load方法):

DataTable dt = OpenWeather10Day.DBQueries.GetHistoricalData(_Zip); 
dt.Columns["Date"].SetOrdinal(0); 
GridView1.DataSource = dt; 
_LinkColIndex = dt.Columns["Edit"].Ordinal; 
_CommentsColIndex = dt.Columns["Comments"].Ordinal; 
GridView1.DataSource = dt.DefaultView; 
GridView1.DataBind(); 

这里是OnRowDataBound功能:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    {//remove double edit column and add a div in comments column to make it the correct size 
     TableCell cell = e.Row.Cells[0]; 
     e.Row.Cells.RemoveAt(0); 
     e.Row.Cells.RemoveAt(_LinkColIndex); 
     e.Row.Cells.Add(cell); 
     if (e.Row.RowType == DataControlRowType.DataRow) { 
      TableCell commentsCell = e.Row.Cells[_CommentsColIndex]; 
      HtmlGenericControl div = new HtmlGenericControl("DIV"); 
      div.Attributes.Add("class", "innerDiv"); 
      div.InnerHtml = commentsCell.Text; 
      commentsCell.Text = string.Empty; 
      commentsCell.Controls.Add(div); 
     } 
    } 

我发现错误是与我的“编辑”列。如果我从表格中删除编辑列并删除所有与之相关的代码,所有300行加载没有问题,并且页面仍处于活动状态。问题在于编辑列对我的页面至关重要,所以这不是一个可行的解决方案。

我已经看过滚动分页,但我找不到一个例子/演示完全符合我的需求或足够简单,让我遵循(我几乎是一个完整的初学者)。我也不认为应该有必要实行分页;如果页面需要几秒钟才能加载,那没问题。我真正的问题在于导致会话退出的堆栈溢出。我不知道是什么导致这种情况发生的编辑列,但不知何故,我需要解决这个问题,以便访问此页面不会退出整个会话。如果这是唯一的选择,我愿意在卷轴上添加分页,但正如我所说,我还没有弄清楚。我也不确定它会解决问题。我很高兴发布任何其他代码等,如果它在所有帮助!

回答

0

你能解释一下你的编辑列吗?它是一个Url,预先格式化的HTML吗?它会做什么?

您正在看到2编辑列,因为您的GridView中已添加了一列,则在绑定数据时添加第二列。使用AutoGenerateColumns属性来防止这种情况,然后添加到其他列中。

<asp:GridView runat="server" ID="GridView1" 
    AutoGenerateColumns="false"> 
    <Columns> 
     <asp:BoundField DataField="Edit" HeaderText="Edit" /> 
     <asp:BoundField DataField="Date" HeaderText="Date" /> 
     <asp:TemplateField HeaderText="Comments"> 
      <ItemTemplate> 
       <div class="innerDiv"> 
        <%# Eval("Comments") %> 
       </div> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

可以使用TemplateField把在HTML到您的评论栏,并摆脱

OnRowDataBound="GridView1_RowDataBound" 
+0

我需要我的专栏是因为自动生成的,这取决于屏幕大小,将显示不同的列(我在表格中有5-10列)。我还需要双编辑列,因为DataTable的编辑列包含网址的HTML,如下所示:编辑,所以如果我让它自动生成,文本文本出现,而是我希望它成为一个链接。因此,我使用表中的数据在页面中创建链接列,然后再从表中删除列。这是我能找到的唯一解决方案。 – issharp