views:

117

answers:

3

I have a web app where everytime I upload an Excel file, its contents appears in Gridview, which is binded with a DataTable. Using the properties, I have set paging and sorting to true.

When I input a file using fileupload and a submit button, the web app has page numbers at the bottom appear. It also correctly sorts it, (my page size is set to 10), however, when i click on the next page (pg 2, for instance), nothing shows up until i upload the another file and hit submit again.

This is the code in which I build the table:

protected void AddResultToGrid(String url, String result)
{
  data = (DataTable)Session["URLSessionData"];
  DataRow dr = data.NewRow();
  dr[0] = url;
  dr[1] = result;
  data.Rows.Add(dr);

  gdvResults.DataSource = data;
  gdvResults.DataBind();
  Session["URLSessionData"] = data;
}

This is the page load:

protected void Page_Load(object sender, EventArgs e)
{
  lblFiup.Text = "";

    data.Columns.Add("URL", typeof(String));
    data.Columns.Add("Status", typeof(String));
    gdvResults.DataSource = data;
    gdvResults.DataBind();

  gdvResults.AllowPaging = true;
  gdvResults.AllowSorting = true;
  if (!Page.IsPostBack)
  {

    Session["URLSessionData"] = data;
  }
}

I think it may have something to do with the page index... but i'm not sure. Here's my page index change method:

    protected void gdvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        e.NewPageIndex = e.NewPageIndex + 1;
        DataBind();

    }

Thanks in advance for your help (:

A: 

I had the same problem. To handle this, try setting the page index in the PageIndexChange event like so:

protected void gdvResults_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 

    gdvResults.PageIndex = e.NewPageIndex; 
    gdvResults.EditIndex = -1;
    DataBind(); 

}

Setting EditIndex is only needed if you will be using edit mode to update values. Otherwise you can exclude that.

Good Luck. Hope this helps :)


I've stripped my program to do nothing but fill the gridview and allow page changing.

namespace EorManager
{
public partial class _Default : System.Web.UI.Page
{
    //Connection to database
    EorLinqClassDataContext eorConnection = new EorLinqClassDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        //On FIRST page load I call my BindGridview method. 
        //Afterward I only call my BindGridview method from events
        if (!IsPostBack)
        {
            BindGridview();
        }
    }

    private void BindGridview()
    {
        //This is where I pull three columns from a table which I want displayed in the gridview. 
        //You will want to upload your excel file in a seperate method and save it locally so you dont have to constantly re-upload it
        var myGridDataSource = from p in eorConnection.EORCategories
                               orderby p.EORCategoryID
                               select new
                               {
                                   EORCategoryID = p.EORCategoryID,
                                   EOR = p.EOR,
                                   CategoryName = p.CategoryName
                               };


        grdEOR.DataSource = myGridDataSource;
        grdEOR.DataBind();

    }

    protected void grdEor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEOR.PageIndex = e.NewPageIndex;
        BindGridview();

    }
}
}
Starwfanatic
Hmm, it's still acting the same way.The second page won't appear until I've uploaded another file.Thanks for your help though!!
loreedee
Ok, in my particular case I was using a database lookup to populate the gridview. In the PageIndexChanging event where you have DataBind(); I call my method BindGridView(); inside BindGridView I query the database for the desired rows which are then databound to the gridview. When I replace BindGridView() with DataBind(), my page change acts as yours does. I believe you just need to call a method like your "AddResultToGrid" to databind the gridview rather than just using DataBind() by itself. Also, you may want to debug and make sure your data is not being cleared before databinding. GL
Starwfanatic
Hmm, I still can't figure it out. Before I added the PageIndexChanging method, it was only AddResultToGrid that was binding the data. I've just been trying to bound it in different methods and such but all returns the same result.What do you mean by "querying the database for desired rows?"TY (:
loreedee
Querying the database is searching the database for the data i want to put in the grid. it would be the equivalent of you re-uploading the file every page change. But you can get around that by saving the uploaded data in a variable in your program and then just accessing the data from there every page change and databinding it. I'll post some of my code tomorrow if you are still unsure. Heading out in a couple mins or I'd post it now.
Starwfanatic
Ah, I see. I think I already have that. I keep it in my Session data.. the Session["URLSessionData"]. The problem I am having is that everytime I click on the second or third page... the grid itself won't show up. However, if I upload another file, the contents from that file will be added onto there, and the pages will automatically update, but again, if I try to navigate to the next page or previous page, it won't appear again.
loreedee
Can you post your code as it appears now?
Starwfanatic
So i added my own BindGridView method containing only datasource and databind and changed it to static. still the same result.i have a feeling it's something really simple, but i can't think of it.do you think it might be the postback or something like that?
loreedee
Possiby, I'd say if your code looks very similar to mine and you cant see where the problem is, make a new project, copy my code, see if it works. If so, you can start with mine and make changes to it until it works like yours. Maybe then you will come across one change where the code will break and you will see where the problem lies. I'll post my GridView code in case you want to try that.
Starwfanatic
Okay. THANK YOU SOOOO MUCH! (:It was the postback thing after all. I saw that you built your grid where the if(!Page.isPostBack) statement was so I figured building only the columns in that wasn't enough.I'll edit my answer to show what it looks like now. Thanks for all of your help!
loreedee
A: 
    public partial class _Event : System.Web.UI.Page
    {
        public HttpWebRequest req;
        public HttpWebResponse resp;
        public String link;
        public String line;
        public String stat;
        public String result;
        public DataTable data = new DataTable();
        public DataTable clrdata = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblFiup.Text = "";
            if (!Page.IsPostBack)
            {                
                data.Columns.Add("URL", typeof(String));
                data.Columns.Add("Status", typeof(String));
                AddResultToGrid("", ""); // this is the only way i know how to do this, as of now...
                data.Rows[0].Delete();

                 Session["URLSessionData"] = data;
                BindGridView();

            }
        }

        private void BindGridView()
        {
            gdvResults.DataSource = data;
            gdvResults.DataBind();
        }

        protected void AddResultToGrid(String url, String result)
        {
            data = (DataTable)Session["URLSessionData"];
            DataRow dr = data.NewRow();
            dr[0] = url;
            dr[1] = result;
            data.Rows.Add(dr);

            gdvResults.DataSource = data;
            gdvResults.AllowPaging = true;
            gdvResults.DataBind();
            Session["URLSessionData"] = data;
        }
loreedee
+1  A: 

Ok, I made a mock datatable for my program. It acted just like you described. the second page was always blank. I set the DataTable to static and it worked. Here's my final code:

namespace EorManager
{
public partial class _Default : System.Web.UI.Page
{
    //Must be static
    static DataTable data = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        //On FIRST page load I call my BindGridview method. 
        //Afterward I only call my BindGridview method from events
        if (!IsPostBack)
        {
            //make a column
            DataColumn myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "url";
            data.Columns.Add(myDataColumn);

            //add rows
            DataRow row;
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.google.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.facebook.com";
            data.Rows.Add(row);
            row = data.NewRow();
            row["url"] = "www.stackoverflow.com";
            data.Rows.Add(row);


            BindGridview();
        }
    }


    private void BindGridview()
    {

        grdEOR.DataSource = data;
        grdEOR.DataBind();

    }

    protected void grdEor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEOR.PageIndex = e.NewPageIndex;
        BindGridview();

    }
}
}

This is my GridView code:

<asp:GridView ID="grdEOR" runat="server" BackColor="White"
            BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
            AllowPaging="True"
            PageSize="15" 
                                          >
            <PagerSettings Mode="NumericFirstLast" />
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2D41F7" />
            <AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black" 
                BorderStyle="Solid" BorderWidth="5px" />
        </asp:GridView>
Starwfanatic