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

时间:2013-11-06 18:09:23

标签: c# asp.net data-binding telerik postback

我们项目的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上,一切似乎都充满了正确的价值。

然而,当在PostBack上加载ASPX页面时 Telerik RadGrid消失了。

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

2 个答案:

答案 0 :(得分:4)

您不应在commentRadGrid.DataBind()事件中致电commentRadGrid_NeedDataSource

Advanced Data-binding (using NeedDataSource event)

  

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

更新

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

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

注意: 如果您只有很少的记录,则需要向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 :(得分:-1)

Just EnableViewState =&#34; true&#34;对于RadGrid控件..网格与Column HeaderText一起完美呈现。