当前位置: 代码迷 >> ASP.NET >> 小白未将对象引用设置到对象的实例
  详细解决方案

小白未将对象引用设置到对象的实例

热度:3762   发布时间:2013-02-25 00:00:00.0
小白求教:未将对象引用设置到对象的实例
做Repeater控件的翻页功能时,出现这个错误提示。
HTML code
<FooterTemplate>     <tr>         <td>      <asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>     <asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>     <asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>     <asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>     当前为[<asp:Label ID="lblCurrentPage" runat="server" Text=""></asp:Label>              页] 共[<asp:Label ID="labPage" runat="server" Text=""></asp:Label> 页]     </td></tr>             </table></FooterTemplate>

绑定Repeater控件代码如下:
 
C# code
private void BindRepeater()    {        string strSqlConnection = "server=.\\sqlexpress;database=dbArticle;uid=sa;pwd=xiaobai;";        string strSql = "select ID,title,Summary from tnews";        SqlConnection conn=new SqlConnection(strSqlConnection);        conn.Open();        SqlCommand cmd = new SqlCommand(strSql,conn);        SqlDataAdapter sda = new SqlDataAdapter(cmd);        DataSet ds = new DataSet();        sda.Fill(ds,"news");        PagedDataSource pds = new PagedDataSource();        pds.DataSource = ds.Tables["news"].DefaultView;        //PagedDataSource aa = new PagedDataSource();        pds.AllowPaging = true;//允许分页        pds.PageSize = 8;//单页显示项数        int CurPage;        if (Request.QueryString["Page"] != null)            CurPage = Convert.ToInt32(Request.QueryString["Page"]);        else            CurPage = 1;        pds.CurrentPageIndex = CurPage - 1;        int Count = pds.PageCount;                ((Label)this.RepterArticle.FindControl("lblCurrentPage")).Text = "当前页:" + CurPage.ToString();//提示错误在这一行,未将对象引用设置到对象的实例。        ((Label)this.RepterArticle.FindControl("labPage")).Text = Count.ToString();   }


------解决方案--------------------------------------------------------
if (e.Item.ItemType == ListItemType.Item) { Label lblID = (Label)e.Item.FindControl("lblID"); }

用上面代码
------解决方案--------------------------------------------------------
Repeater 的绑定在那里呢
------解决方案--------------------------------------------------------
探讨
if (e.Item.ItemType == ListItemType.Item) { Label lblID = (Label)e.Item.FindControl("lblID"); }

用上面代码

------解决方案--------------------------------------------------------
你要生成这个控件,是在数据绑定上去以后才有的,你还没有绑定直接去找怎么找的到?
在你的Repeater中加事件在数据绑定上以后再找 OnItemDataBound="CeShi_ItemDataBound"

protected void CeShi_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{

((Label)e.Item.FindControl("lblCurrentPage")).Text = "1";//这里可以写你的页面
}
}
  相关解决方案