首页 新闻 赞助 找找看

GridView绑定ObjectDataSource无效

1
悬赏园豆:100 [已关闭问题] 关闭于 2012-03-01 12:59

GridView启用分页后绑定ObjectDataSource(ObjectDataSource也启用了)数据查不出、但对象中方法的确被调用,而且有数据(断点看了),ObjectDataSource禁用分页可以查出数据(我定义了非分页版的方法)。问题就是不能启用ObjectDataSource分页,SelectCountMethod之类的方法我是都指定了的。

上代码:

 

<asp:GridView CssClass="grid" ID="gvScreen" AllowPaging="true" PageSize="20"
            GridLines="None" AutoGenerateColumns="false"
            DataSourceID="srcBBS" runat="server" ondatabinding="gvScreen_DataBinding"
            ondatabound="gvScreen_DataBound" >
            <Columns>
                <asp:TemplateField HeaderText="发布者">
                    <ItemTemplate>
                        <span class="user">
                            <%#Eval("Name") %></span><br />
                        <span class="ipaddress">
                            <%#HideIpAddress(Convert.ToString(Eval("IpAddress")))%></span>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="发布时间">
                    <ItemTemplate>
                        <span class="time">
                            <%#Eval("Time") %></span></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="发布内容">
                    <ItemTemplate>
                        <div class="content">
                            <%#Eval("Body") %></div>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                没有留言。</EmptyDataTemplate>
        </asp:GridView>
        <asp:ObjectDataSource ID="srcBBS" EnablePaging="true" TypeName="BLL.BBSLogic"
            InsertMethod="Insert" SelectCountMethod="GetCount"  SelectMethod="GetPosts"
            runat="server" DataObjectTypeName="Model.Post"
            OnInserting="srcBBS_Inserting">
        </asp:ObjectDataSource>

 

 

public static class BBSLogic
    {
        static IBBSDataAccess bbsDA = new BBSSqlDataAccess();

        public static List<Post> GetPosts()
        {
            return bbsDA.GetPosts();
        }

        public static List<Post> GetPosts(long maximumRows, long startRowIndex)
        {
            return bbsDA.GetPosts(maximumRows,startRowIndex);
        }

        public static bool Insert(Post post)
        {
            ValidationPost(post);
            return bbsDA.Insert(post);
        }

        public static long GetCount()
        {
            return bbsDA.GetCount();
        }

        public static void ValidationPost(Post post)
        {
            if (post.Body.Length > 300 || post.Body.Length <= 0)
                throw new ArgumentException("Body属性长度应该大于0小于300");
            if (post.IpAddress.Length > 50)
                throw new ArgumentException("IpAddress长度不能超过50");
            if (post.Name.Length > 50)
                throw new ArgumentException("Name长度不能大于50");
        }
    }

 

我自己调试的时候断点了GridView的DataBanding和DataBound处理函数

结果发现在这两个函数里GridView的Row.Count都是0,而且用ObjectDataSource的Select方法查不到数据(我添加监视中查不到,但代码的确执行过、而却有返回的内容)。

我把ObjectDataSource的EnabledPaging设为false调用非分页版的方法发现在DataBound中ROW.COUNT是产生变化的

然后我启用ObjectDataSource的分页,然后在分页方法中直接调用非分页的方法,结果这两个函数中Row.Count没有变化,但是在监视里ObjectDataSource的Select可以查到数据。

总之就是只要启用ObjectDataSource的分页GridView就是空,求解决办法

文学爷们的主页 文学爷们 | 初学一级 | 园豆:15
提问于:2012-03-01 01:33
< >
分享
所有回答(2)
1

  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        InitPage(); //重新绑定GridView数据的函数
    }

你添加上面的分页事件试试。

悟行 | 园豆:12559 (专家六级) | 2012-03-01 09:34
0

是因为我的SelectCountMethod返回是long.返回int即可

文学爷们 | 园豆:15 (初学一级) | 2012-03-01 12:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册