How to Change the TotalRowCount ...

2 posts, 0 answers
  1. gzzn
    gzzn avatar
    12 posts
    Member since:
    Jan 2011

    Posted 28 Jul 2011 Link to this post

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Data" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
    <head runat="server">
    <title>How to Change the TotalRowCount ...</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <script runat="server" language="c#">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.RadGrid1.DataSource = DataPage(0);
            this.RadGrid1.DataBind();
        }
    }
    public DataRow[] DataPage(int PageIndex)
    {
        string sql = string.Empty;
        switch (this.RadComboBox1.SelectedValue)
        {
            case "2":
                sql = "pkID >= " + (PageIndex * 5 + 1) + " AND pkID <= " + (PageIndex * 5 + 5) + "";
                return dt2().Select(sql);
            default:
                sql = "pkID >= " + (PageIndex * 5 + 1) + " AND pkID <= " + (PageIndex * 5 + 5) + "";
                return dt1().Select(sql);
        }
    }
    protected void RadComboBox1_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        this.RadGrid1.DataSource = DataPage(this.RadDataPager1.CurrentPageIndex);
        this.RadGrid1.DataBind();
    }
    protected void RadDataPager1_PageIndexChanged(object sender, RadDataPagerPageIndexChangeEventArgs e)
    {
        this.RadGrid1.DataSource = DataPage(e.NewPageIndex);
        this.RadGrid1.DataBind();
    }
    protected void RadDataPager1_TotalRowCountRequest(object sender, RadDataPagerTotalRowCountRequestEventArgs e)
    {
        this.RadDataPager1.TabIndex = 1;
        switch (this.RadComboBox1.SelectedValue)
        {
            case "1":
                e.TotalRowCount = dt1().Rows.Count;
                break;
            case "2":
                e.TotalRowCount = dt2().Rows.Count;
                break;
        }
    }
    public static DataTable dt1()
    {
        DataTable table = new DataTable();
        table.Columns.Add("pkID", typeof(Int32));
        table.Columns.Add("ParentID", typeof(Int32));
        table.Columns.Add("Name", typeof(String));
        table.Columns.Add("Text", typeof(String));
        table.Rows.Add(new object[] { 1, 1, "testName1", "testText1" });
        table.Rows.Add(new object[] { 2, 1, "testName2", "testText2" });
        table.Rows.Add(new object[] { 3, 1, "testName3", "testText3" });
        table.Rows.Add(new object[] { 4, 1, "testName4", "testText4" });
        table.Rows.Add(new object[] { 5, 1, "testName5", "testText5" });
        table.Rows.Add(new object[] { 6, 1, "testName6", "testText6" });
        table.Rows.Add(new object[] { 7, 1, "testName7", "testText7" });
        table.Rows.Add(new object[] { 8, 1, "testName8", "testText8" });
        table.Rows.Add(new object[] { 9, 1, "testName9", "testText9" });
        table.Rows.Add(new object[] { 10, 1, "testName10", "testText10" });
        table.Rows.Add(new object[] { 11, 1, "testName11", "testText11" });
        table.Rows.Add(new object[] { 12, 1, "testName12", "testText12" });
        return table;
    }
    public static DataTable dt2()
    {
        DataTable table = new DataTable();
        table.Columns.Add("pkID", typeof(Int32));
        table.Columns.Add("Name", typeof(String));
        table.Rows.Add(new object[] { 1, "C" });
        table.Rows.Add(new object[] { 2, "C++" });
        table.Rows.Add(new object[] { 3, "C#" });
        table.Rows.Add(new object[] { 4, "Java" });
        table.Rows.Add(new object[] { 5, "F++" });
        table.Rows.Add(new object[] { 6, "Z++" });
        return table;
    }
    </script>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadComboBox1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadComboBox1" />
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    <telerik:AjaxUpdatedControl ControlID="RadDataPager1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadDataPager1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadDataPager1" />
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadComboBox ID="RadComboBox1" Runat="server" AutoPostBack="true" onselectedindexchanged="RadComboBox1_SelectedIndexChanged">
        <Items>
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1" Value="1" Selected="true" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2" Value="2" />
        </Items>
    </telerik:RadComboBox>
    <telerik:RadGrid ID="RadGrid1" runat="server" PageSize="5">
    </telerik:RadGrid>
    <telerik:RadDataPager ID="RadDataPager1" runat="server" PageSize="5" 
        ontotalrowcountrequest="RadDataPager1_TotalRowCountRequest" 
        onpageindexchanged="RadDataPager1_PageIndexChanged">
        <Fields>
            <telerik:RadDataPagerButtonField FieldType="NextLast" HorizontalPosition="RightFloat" />
            <telerik:RadDataPagerButtonField FieldType="Numeric" PageButtonCount="10" HorizontalPosition="RightFloat" />
            <telerik:RadDataPagerButtonField FieldType="FirstPrev" HorizontalPosition="RightFloat" />
            <telerik:RadDataPagerGoToPageField CurrentPageText="" TotalPageText="/" TextBoxWidth="38" SubmitButtonText="Go" HorizontalPosition="RightFloat" />
        </Fields>
    </telerik:RadDataPager>
    </form>
    </body>
    </html>

    this web application is correctly. just when i change RadComboBox Controls Item,
    the TotalRowCount cannot be changed. how can i do ?
    e.g.:
    when the page load, default RadComboBox Item's Value is 1. then the current DataTable is dt1, the table's rows number is 12.
    when i change RadComboBox to item 2. it's Value is 2,  the current DataTable is dt2. the table's rows number is 6.
    but the RadDataPager's TotalRowCount still keep old value: 12 rows.

    thanks.
  2. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 01 Aug 2011 Link to this post

    Hi Gzzn,

    Unfortunately, that scenario is not supported. The main problem is that OnTotalRowCountRequest is fired before OnSelectedIndexChanged, so when creating the RadDataPager, it uses the old value from the RadComboBox. After that, when invoking DataPage(), the value is changed and the Rebind() is successful.
     
    However, to achieve the desired functionality, you could use the custom paging feature of the RadGrid. Online demos, you could find here and here.

    Regards,
    Andrey
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top