This is a migrated thread and some comments may be shown as answers.

Setting Grid Page with Combobox

2 Answers 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KenF
Top achievements
Rank 1
KenF asked on 10 Feb 2009, 02:16 AM
I want a custom pager template that will change the page via the SelectedIndexChanged event of a combobox.  I've seen all the demos and threads on pager templates, but none on changing the page with a combobox.  Basically, I would like to fill the combobox with values from 1 to NumberOfPages.  Then, when a user selects a page number from the combobox, the grid will set to that page.  The combobox's selectedvalue should match the current page once selected. I can't seem to figure this out.  I'm using VB.  Can anyone provide any direction, or better yet, code examples of how to accomplish this?

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 10 Feb 2009, 06:14 AM
Hello,

Give a try with the following approach and see whether it helps.

ASPX:
 <PagerTemplate> 
                    <telerik:RadComboBox ID="RadComboBox2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadComboBox2_SelectedIndexChanged"
                        <Items> 
                           <telerik:RadComboBoxItem Text="0" Value="0" />  
                          <telerik:RadComboBoxItem Text="1" Value="1" />  
                          <telerik:RadComboBoxItem Text="2" Value="2" />  
                          <telerik:RadComboBoxItem Text="3" Value="3" />  
                          <telerik:RadComboBoxItem Text="4" Value="4" />  
                          </Items> 
                    </telerik:RadComboBox> 
                </PagerTemplate> 

CS:
  Protected Sub RadComboBox2_SelectedIndexChanged(ByVal o As ObjectByVal e As RadComboBoxSelectedIndexChangedEventArgs) 
     Dim combo As RadComboBox = DirectCast(o, RadComboBox) 
     RadGrid1.CurrentPageIndex = Integer.Parse(combo.SelectedItem.Text) 
     ViewState("Selected") = combo.SelectedIndex 
     RadGrid1.MasterTableView.Rebind() 
 End Sub 
 
 Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As EventArgs) 
     For Each pageritem As GridPagerItem In RadGrid1.MasterTableView.GetItems(GridItemType.Pager) 
         Dim combo As RadComboBox = DirectCast(pageritem.FindControl("RadComboBox2"), RadComboBox) 
         combo.SelectedIndex = Convert.ToInt32(ViewState("Selected")) 
         
     Next 
     
 End Sub 


Thanks
Shinu




0
KenF
Top achievements
Rank 1
answered on 10 Feb 2009, 02:27 PM
Shinu,
Thank you very much for the help.  The code sample you provided was extremely useful.  Using your example, plus other code examples found on Telerik forums, and with a bit more tweaking, I was able to get the effect I was looking for.  In case others would like to use a page number combobox in a pager template, I have included the final version of the code here:

ASPX:

 

<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
    <Telerik:RadGrid ID="RadGrid1" AllowPaging="true" runat="server">     
        <MasterTableView>             
            <PagerTemplate> 
                <table border="0" cellpadding="5" width="100%">  
                 <tr>                      
                 <td style="border-style:none;">      
                    <asp:Label ID="lblPage" SkinID="Title" runat="server"   Text="Page" />                
                    <asp:Label ID="lblCurrentPageNumber" SkinID="Title" runat="server" Text='<%# DataBinder.Eval(Container, "OwnerTableView.CurrentPageIndex") + 1 %>' /> 
                    <asp:Label ID="lblOf" runat="server" SkinID="Title" Text="of" />     
                    <asp:Label ID="lblRowCount" SkinID="Title" runat="server" Text'<%# DataBinder.Eval(Container, "Paging.PageCount") %>'/>   
                    <asp:Label ID="lbldash" runat="server" SkinID="Title" Text=" - " /> 
                    <asp:Label ID="lblNumOfRecords" SkinID="Title" runat="server" Text='<%# DataBinder.Eval(Container, "Paging.DataSourceCount") %>' />     
                    <asp:Label ID="lblRecords" SkinID="Title" runat="server" Text="Records" />      
                  </td> 
                  <td style="border-style:none;">  
                      <asp:ImageButton ID="imgButtonFirst" runat="server" ImageAlign="Middle"   
                        ImageUrl="images/first.gif" CommandArgument="First" CommandName="Page" /> 
                    &nbsp;  
                      <asp:ImageButton ID="imgButtonPrev" runat="server" ImageAlign="Middle" ImageUrl="images/prev.gif" CommandArgument="Prev" CommandName="Page" />                
                      <asp:Image ID="Image1" runat="server" ImageUrl="images/spacer.gif" /> 
                    &nbsp;<telerik:RadComboBox ID="cbPageNumber" runat="server" 
                             AutoPostBack="true" Width=".75in" OnSelectedIndexChanged="cbPageNumber_OnSelectedIndexChanged">  
                        </telerik:RadComboBox> 
 
                      <asp:Image ID="Image2" runat="server" ImageUrl="images/spacer.gif" /> 
                      <asp:ImageButton ID="imgButtonNext" runat="server" ImageAlign="Middle" ImageUrl="images/next.gif" CommandArgument="Next" CommandName="Page" /> 
   
 
                    &nbsp;  
                      <asp:ImageButton ID="imgButtonLast" runat="server" ImageAlign="Middle" ImageUrl="images/last.gif" CommandArgument="Last" CommandName="Page" />          
                        
                  </td> 
                    
                  <td style="border-style:none;">  
                     <asp:Label ID="lblRecordsPerPage" runat="server" Text="Records per page" /> 
                      <asp:TextBox ID="tbRecordsPerPage" runat="server" 
                        Columns="1" AutoPostBack="true" 
                        Text='<%# DataBinder.Eval(Container, "Paging.Count") %>' 
                        OnTextChanged="tbRecordsPerPage_TextChanged">  
                    </asp:TextBox> 
                 </td>                       
                 </tr> 
                </table>   
                              
            </PagerTemplate> 
      </MasterTableView> 
    </Telerik:RadGrid> 
      
    </div> 
    </form> 
</body> 
 

VB Code behind:
Imports System.Data  
Imports Telerik.Web.UI  
 
Partial Public Class Default3  
    Inherits System.Web.UI.Page  
 
    Protected Sub cbPageNumber_OnSelectedIndexChanged(ByVal sender As ObjectByVal e As RadComboBoxSelectedIndexChangedEventArgs)  
        Dim combo As RadComboBox = DirectCast(sender, RadComboBox)  
        RadGrid1.CurrentPageIndex = Integer.Parse(combo.SelectedItem.Text) - 1  
        RadGrid1.MasterTableView.Rebind()  
    End Sub 
 
    Private Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated  
        If TypeOf e.Item Is GridPagerItem Then 
            Dim pagerItem As GridPagerItem = CType(e.Item, GridPagerItem)  
            Dim imgFirstButton As ImageButton = CType(pagerItem.FindControl("imgButtonFirst"), ImageButton)  
            Dim imgPrevButton As ImageButton = CType(pagerItem.FindControl("imgButtonPrev"), ImageButton)  
            Dim imgNextButton As ImageButton = CType(pagerItem.FindControl("imgButtonNext"), ImageButton)  
            Dim imgLastButton As ImageButton = CType(pagerItem.FindControl("imgButtonLast"), ImageButton)  
            If pagerItem.OwnerTableView.CurrentPageIndex = "0" Then 
                imgFirstButton.Enabled = False 
                imgPrevButton.Enabled = False 
                imgNextButton.Enabled = True 
                imgLastButton.Enabled = True 
            ElseIf pagerItem.OwnerTableView.CurrentPageIndex + 1 = pagerItem.OwnerTableView.PageCount Then 
                imgFirstButton.Enabled = True 
                imgPrevButton.Enabled = True 
                imgNextButton.Enabled = False 
                imgLastButton.Enabled = False 
            Else 
                imgFirstButton.Enabled = True 
                imgPrevButton.Enabled = True 
                imgNextButton.Enabled = True 
                imgLastButton.Enabled = True 
            End If 
 
        End If 
 
    End Sub 
 
    Private Sub RadGrid1_NeedDataSource(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        Dim dt As New DataTable()  
        Dim dr As DataRow  
        Dim cols As String() = {"Column1""Column2""Column3""Column4"}  
 
        For Each col As String In cols  
            dt.Columns.Add(col)  
        Next 
 
        Dim i As Integer = 1  
        While i <= 50  
            dr = dt.NewRow()  
 
            Dim k As Integer = 0  
            While k < cols.Length  
                dr(cols(k)) = [String].Format("{0} row {1}", cols(k), i)  
                System.Math.Max(System.Threading.Interlocked.Increment(k), k - 1)  
            End While 
            dt.Rows.Add(dr)  
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)  
        End While 
 
        TryCast(sender, RadGrid).DataSource = dt  
    End Sub 
 
    Private Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
        For Each pagerItem As GridPagerItem In RadGrid1.MasterTableView.GetItems(GridItemType.Pager)  
            Dim cbPageNumber As RadComboBox = DirectCast(pagerItem.FindControl("cbPageNumber"), RadComboBox)  
            cbPageNumber.Items.Clear()  
            For i As Integer = 0 To pagerItem.OwnerTableView.PageCount - 1  
                Dim intPageNumber As Integer = i + 1  
                Dim cbItem As RadComboBoxItem = New RadComboBoxItem(intPageNumber.ToString())  
                cbPageNumber.Items.Add(cbItem)  
                If i = pagerItem.OwnerTableView.CurrentPageIndex Then 
                    cbItem.Selected = True 
                End If 
            Next 
 
        Next 
    End Sub 
 
    Protected Sub tbRecordsPerPage_TextChanged(ByVal sender As ObjectByVal e As System.EventArgs)  
        Dim tbRecordsPerPage As TextBox = DirectCast(sender, TextBox)  
        Dim NewNumOfRows As String = tbRecordsPerPage.Text  
        RadGrid1.MasterTableView.PageSize = NewNumOfRows  
        RadGrid1.MasterTableView.Rebind()  
    End Sub 
End Class 

Shinu, thank you very much for your help!

-kenf-

 

Tags
Grid
Asked by
KenF
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
KenF
Top achievements
Rank 1
Share this question
or