首页 文章

为什么Telerik RadGrid在PostBack上消失了?

提问于
浏览
0

我们项目的Telerik DLL版本是2012.3.1016.40

我们在一个ASP.NET页面中使用Telerik RadGrid . 我们将Telerik RadGrid绑定到ASP.NET DataTable对象 . 我们还有一个ASP.NET按钮,如果单击它就会关闭页面 .

我们调用基于REST的Web服务方法,并使用返回的数据填充ASP.NET DataTable对象 .

我们将ASP.NET DataTable对象绑定到Telerik RadGrid .

使用OnNeedDataSource方法填充ASP.NET DataTable对象 .

protected void commentRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
        try
        {

             // The ViewStateThreadedDiscussion has a Type of ASP.NET DataTable.
             // Blah Blah Code That Populates the ViewStateThreadedDiscussion DataTable Blah Blah

             commentRadGrid.DataSource = new string[] { };

            if (ViewStateThreadedDiscussion == null)
            {
                // Extremely Important to use empty string double quotes if
                // the threadedDiscussionWithinDataTable DataTable is null because
                // Telerik only works properly if you assign the Telerik RadGrid DataSource
                // to an empty string with double quotes when we have a DataTable variable that is null.
                // Never assign null to the Telerik RadGrid DataSource because Telerik DLL throws Error.

               commentRadGrid.DataSource = "";
            }
            else
            {
                commentRadGrid.DataSource = ViewStateThreadedDiscussion;
            }



        } // end of try
        catch (Exception ex)
        {
            log4NetInstance.Error(ex.Message);
            log4NetInstance.Error(ex.StackTrace);
            log4NetInstance.Error(ex.ToString());
        } // end of catch (Exception ex)
    } // end of protected void SharedWithRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

我有一个指定了OnClick方法的Telerik RadButton . 在Telerik RadButton OnClick方法中,我在Telerik RadGrid上调用Rebind . 这是Telerik RadButton OnClick方法代码:

protected void OlderCommentsButton_Click(object sender, EventArgs e)
    {
        try
        {

            commentRadGrid.Rebind();
        }
        catch (Exception ex)
        {
            log4NetInstance.Error(ex.Message);
            log4NetInstance.Error(ex.StackTrace);
            log4NetInstance.Error(ex.ToString());
        } // end of catch (Exception ex)
    } // end of protected void OlderCommentsButton_Click(object sender, EventArgs e)

这是Telerik RadGrid的声明和配置:

<telerik:RadGrid runat="server" Width="60%" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  HeaderStyle-Width="60%" ID="commentRadGrid" AllowFilteringByColumn="true" AutoGenerateColumns="false"
    AllowPaging="true" OnPageIndexChanged="commentRadGrid_PageIndexChanged"  OnNeedDataSource="commentRadGrid_NeedDataSource" PageSize="100" Skin="Default" AllowSorting="true" AutoPostBack="true" ShowStatusBar="true" AllowCustomPaging="True"
    GridLines="none">

我有Visual Studio 2012,我在调试模式下使用断点运行应用程序 . 我使用Visual Studio 2012提供的“添加监视”功能来处理各种变量,例如ViewStateThreadedDiscussion ASP.NET DataTable和Telerik RadGrid DataSource属性 . 即使在Post Post上,一切似乎都充满了正确的 Value .

但是,当在PostBack上加载ASPX页面时,Telerik RadGrid会消失 .

为什么Telerik RadGrid在PostBack上消失了?

2 回答

  • 4

    你不应该在 commentRadGrid_NeedDataSource 事件中调用 commentRadGrid.DataBind() .

    Advanced Data-binding (using NeedDataSource event)

    你永远不应该在NeedDataSource事件处理程序中调用Rebind()方法 . 当通过NeedDataSource使用高级数据绑定时,你也不应该调用DataBind() . 对于类似Microsoft GridView的数据绑定,请参阅简单数据绑定 .

    更新:

    如果设置 AllowCustomPaging="True" ,则需要提供 VirtualItemCount .

    它基本上意味着你给RadGrid最多100行( PageSize="100" )虽然你有一百万条记录 . 因此,RadGrid需要知道您有多少总记录才能显示分页 .

    Note: 如果您只有很少的记录,则需要向RadGrid提供所有记录 . 如果是这样,您不需要 AllowCustomPaging="True"VirtualItemCount .

    以下是示例示例 -

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <telerik:RadGrid runat="server" 
        ID="commentRadGrid" 
        AllowFilteringByColumn="true" 
        AutoGenerateColumns="True"
        AllowPaging="true" 
        OnPageIndexChanged="commentRadGrid_PageIndexChanged" 
        OnNeedDataSource="commentRadGrid_NeedDataSource" 
        PageSize="100" Skin="Default" 
        AllowSorting="true" 
        ShowStatusBar="true" 
        AllowCustomPaging="True"
        GridLines="none">
    </telerik:RadGrid>
    <asp:Button runat="server" ID="OlderCommentsButton" 
        OnCommand="OlderCommentsButton_Click" Text="Post Back" />
    
    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    protected void commentRadGrid_NeedDataSource(
        object sender, GridNeedDataSourceEventArgs e)
    {
        var users = new List<User>
        {
            new User {FirstName = "John", LastName = "Doe"},
            new User {FirstName = "Jenny", LastName = "Doe"},
        };
    
        commentRadGrid.DataSource = users;
        commentRadGrid.MasterTableView.VirtualItemCount = users.Count;
    }
    
    protected void OlderCommentsButton_Click(object sender, EventArgs e)
    {
        try
        {
            commentRadGrid.Rebind();
        }
        catch (Exception ex)
        {
        }
    }
    
    protected void commentRadGrid_PageIndexChanged(
        object sender, GridPageChangedEventArgs e)
    {
    
    }
    
  • -1

    对于RadGrid控件,只需EnableViewState =“true” . 网格与Column HeaderText一起完美呈现 .

相关问题