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.
W开发者_如何转开发hen 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 (:
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;
}
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>
精彩评论