Data grid with current page combo box selector

Discussion in 'ASP .Net' started by Brian Henry, Jul 19, 2004.

  1. Brian Henry

    Brian Henry Guest

    Hi,

    I have a data grid that is set up like this

    Page items displayed = 10
    EnableViewState = false (i dont want to send large amounts of data over the
    internet!)
    CustomPaging = false
    AllowPaging = true

    now at the bottom of the data grid page, i have the numbers showing up as
    the paging method, i got it to switch pages and show the proper page when
    enableviewstate = false was on, but what I need to do now is show a combo
    box above the grid that says Page x of y is the current one showing then in
    the list show all the pages availabe to select... that part I have done... i
    based it on the pages count in the grid and the value of the item for the
    combo box is the page index of the grid that page is associated with... but
    all the attemps at making the newly selected item in the combo box change
    the current page in the data grid have failed... i tried changeing the data
    grid's curent page index and got nothing out of it... how would you go about
    doing this?! thanks
    Brian Henry, Jul 19, 2004
    #1
    1. Advertising

  2. Hi Brian,

    As for the DataGrid paging problem without ViewState Enabled, I think the
    most important thing is to rebind the grid with data when there is any
    event fired which indicate us to change the grid's page index. For example,
    in the datagrid's PageIndexChanged event, do the following things in
    sequence:

    grid.CurrentPageIndex = e.NewPageIndex;
    ReBindGridWithData();

    And since you use a dropdownlist to let the user select a certain page
    index to change the page index, you also need to do the samething in the
    dropdownlist's SelectedIndexChange event, such as:

    get selected index from the dropdownlist
    set it to the datagrid's CurrentPageIndex property
    Rebind grid with data

    and here is a test page I've made, you can have a test on your side if you
    have anything unclear:
    ==============aspx====================
    <HTML>
    <HEAD>
    <title>PageSelector</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="100%" align="center">
    <tr>
    <td style="HEIGHT: 44px">CurrentPage:<asp:dropdownlist id="lstCurrent"
    EnableViewState="True" Runat="server"
    AutoPostBack="True"></asp:dropdownlist>
    Page Count:
    <asp:label id="lblPageCount" Runat="server"></asp:label></td>
    </tr>
    <tr>
    <td><asp:datagrid id="dgPage" runat="server" EnableViewState="False"
    AllowPaging="True" PageSize="5">
    <PagerStyle Mode="NumericPages"></PagerStyle>
    </asp:datagrid></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>

    =========code behind===================
    public class PageSelector : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList lstCurrent;
    protected System.Web.UI.WebControls.Label lblPageCount;
    protected System.Web.UI.WebControls.DataGrid dgPage;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    Session["DATASOURCE"] = GetDataSource();
    }

    Bind_Grid();
    }

    protected DataTable GetDataSource()
    {
    DataTable tb = new DataTable();
    tb.Columns.Add("index");
    tb.Columns.Add("name");
    tb.Columns.Add("price",typeof(double));

    DataRow dr = null;
    bool[] flags = {true,false};
    for(int i=1;i<=27;++i)
    {
    dr = tb.NewRow();
    dr["index"] = i.ToString();
    dr["name"] = "Name" + i.ToString();
    dr["price"] = 3.434 * (i%3 +1);

    tb.Rows.Add(dr);
    }

    return tb;
    }

    protected void Bind_Grid()
    {
    DataTable tb =(DataTable)Session["DATASOURCE"];
    dgPage.DataSource = tb;
    dgPage.DataBind();

    if(!IsPostBack)
    {
    for(int i=1;i<=dgPage.PageCount;++i)
    {
    lstCurrent.Items.Add(i.ToString());
    }
    lblPageCount.Text = dgPage.PageCount.ToString();
    }

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.lstCurrent.SelectedIndexChanged += new
    System.EventHandler(this.lstCurrent_SelectedIndexChanged);
    this.dgPage.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPage_PageIn
    dexChanged);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void dgPage_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    dgPage.CurrentPageIndex = e.NewPageIndex;
    lstCurrent.SelectedIndex = e.NewPageIndex;
    Bind_Grid();

    }

    private void lstCurrent_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
    int index = lstCurrent.SelectedIndex;

    dgPage.CurrentPageIndex = index;
    Bind_Grid();
    }

    }
    =====================================================

    Also, if you have any other quetsions, please feel free to post here.
    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jul 20, 2004
    #2
    1. Advertising

  3. Hi Brian,

    Have you had a chance to check out the suggestions in my last reply or have
    you got any further ideas on this issue? If you have anything unclear or if
    there're anything else we can help, please feel free to post here. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jul 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    610
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. Adrian
    Replies:
    1
    Views:
    219
    Bullschmidt
    Feb 18, 2004
  3. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    588
    Bob Barrows [MVP]
    Nov 16, 2005
  4. Mike

    Combo Box and text box

    Mike, Sep 24, 2003, in forum: Javascript
    Replies:
    2
    Views:
    157
  5. SM
    Replies:
    2
    Views:
    365
Loading...

Share This Page