<asp:GridView ID="GridViewNews" ShowHeader="False" BorderStyle="None" 
                BorderWidth="0px" Width="100%" AllowPaging="True" PageSize="50" 
                AutoGenerateColumns="False" DataSourceID="ObjectDataSourceNews" 
                DataKeyNames="NewsId" CssClass="icon" PagerStyle-CssClass="page" runat="server" 
                onrowcreated="GridViewNews_RowCreated" >
            <PagerSettings Mode="NumericFirstLast" FirstPageText="首页" LastPageText="末页" PreviousPageText="上一页" NextPageText="下一页" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <dd><span><a href='NewsList.aspx?Id=<%# Eval("CategoryId") %>' title="" target="_blank">[<%# ShowCategory(Convert.ToInt32(Eval("CategoryId")))%>]</a>&nbsp;<a href='NewsDetails.aspx?Id=<%# Eval("NewsId") %>' class='<%#ShowNew(Convert.ToDateTime(Eval("UpdateDate"))) %>' title="" target="_blank"><%# Eval("Title") %></a></span><%# Convert.ToDateTime(Eval("UpdateDate")).ToString("yy-M-d HH:mm") %></dd>
                        <asp:Label ID="Label1" runat="server" Text="" ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>  
            </Columns>
<PagerStyle CssClass="page"></PagerStyle>
        </asp:GridView>
    int Rowindex = 0;
    protected void GridViewNews_RowCreated(object sender, GridViewRowEventArgs e)
    {
        Rowindex ++;
       string valu = ((Label)e.Row.FindControl("Label1")).Text;
       if (Rowindex == 5)
       {
              
           valu = "_____________";
            Rowindex = 0;
         }
    }“/”应用程序中的服务器错误。
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:行 53:     {
行 54:         Rowindex ++;
行 55:        string valu = ((Label)e.Row.FindControl("Label1")).Text;
行 56:        if (Rowindex == 5)
行 57:        {
各位大神们帮小弟看看吧

解决方案 »

  1.   


            Label lab = ((Label)e.Row.FindControl("Label1"));
            if (Rowindex == 5)
            {            lab.Text = "_____________";
                Rowindex = 0;
            }这样应该不会报这个System.NullReferenceException错误了
      

  2.   

    判断Label1是后存在
     Label lab = ((Label)e.Row.FindControl("Label1"));
    if(lab!=null)
    {
            if (Rowindex == 5)
            {            lab.Text = "_____________";
                Rowindex = 0;
            }
    }
      

  3.   

     public int i = 0;//计数,设置分隔符 
    /// <summary>
    /// 设置列表分隔符
    /// </summary>
    /// <returns></returns>
    public string setSeparator()
    {
        i++;
        string strSep = string.Empty;
         if (i % 5 == 0)
          {
           strSep = "<li><div class=\"line_n\"></div></li>";
          }
        return strSep;
    }-------------------------------------------------------<ItemTemplate>
                                    <li>
                                        <div class="left">
                                            <span class="gray01">·</span><a href="service_show_<%#Eval("ArticleID") %>.html"><%#Eval("Title") %></a></div>
                                        <div class="right font_fam01 gray01">
                                            [<%#Eval("AddTime","{0:yyyy-MM-dd}") %>]</div>
                                    </li>
                                    <%=setSeparator() %>
                                </ItemTemplate>