473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DataGrid DataSource is null on postback

If I create a datagrid, and set a source and bind it, the data shows up
fine. When a postback occurs, the DataGrid.DataSource == null. Is there
something in the web.config I'm missing?

This is on asp.net 2.0.

Dec 24 '05 #1
4 5195
Do you bind it declaratively or with code, and if so, what is your code?

--

Riki

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
If I create a datagrid, and set a source and bind it, the data shows up
fine. When a postback occurs, the DataGrid.DataSource == null. Is there
something in the web.config I'm missing?

This is on asp.net 2.0.

Dec 24 '05 #2
With code. In the PageIndexChanged event, the datagrid's datasource is
null.

public partial class DataGrid : System.Web.UI.Page
{
private System.Collections.ArrayList list;

protected void Page_Load(object sender, EventArgs e)
{
if( !Page.IsPostBack )
{
list = new ArrayList();
list.Add( "one" );
list.Add( "two" );
list.Add( "three" );
list.Add( "four" );
list.Add( "five" );

this.DataGrid1.DataSource = list;
this.DataGrid1.DataBind();
}
}

protected void DataGrid1_PageIndexChanged( object source,
DataGridPageChangedEventArgs e )
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
}

Dec 24 '05 #3
B
Hello,

From you code it looks that you are initializing list only once when page
loads first time. So when page loads second time It want be able to find
variable list.

Just check following code.
protected void Page_Load(object sender, EventArgs e)
{
list = new ArrayList();
list.Add( "one" );
list.Add( "two" );
list.Add( "three" );
list.Add( "four" );
list.Add( "five" );
if( !Page.IsPostBack )
{

this.DataGrid1.DataSource = list;
this.DataGrid1.DataBind();
}
}
B

"Narshe" wrote:
With code. In the PageIndexChanged event, the datagrid's datasource is
null.

public partial class DataGrid : System.Web.UI.Page
{
private System.Collections.ArrayList list;

protected void Page_Load(object sender, EventArgs e)
{
if( !Page.IsPostBack )
{
list = new ArrayList();
list.Add( "one" );
list.Add( "two" );
list.Add( "three" );
list.Add( "four" );
list.Add( "five" );

this.DataGrid1.DataSource = list;
this.DataGrid1.DataBind();
}
}

protected void DataGrid1_PageIndexChanged( object source,
DataGridPageChangedEventArgs e )
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
}

Dec 26 '05 #4
That is perfectly normal since data source of a control is not stored over
postback since control already saves its state with ViewState (and also
control state in asp.net v2.0). So for performance reasons, it is not kept
by default (control itself is restored from viewstate)

You need to refetch the data to bind the control again (note: you certainly
can use state mechanisms in asp.net like session or cache to keep the data
available)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Narshe" <na****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
With code. In the PageIndexChanged event, the datagrid's datasource is
null.

public partial class DataGrid : System.Web.UI.Page
{
private System.Collections.ArrayList list;

protected void Page_Load(object sender, EventArgs e)
{
if( !Page.IsPostBack )
{
list = new ArrayList();
list.Add( "one" );
list.Add( "two" );
list.Add( "three" );
list.Add( "four" );
list.Add( "five" );

this.DataGrid1.DataSource = list;
this.DataGrid1.DataBind();
}
}

protected void DataGrid1_PageIndexChanged( object source,
DataGridPageChangedEventArgs e )
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
}

Dec 26 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
1
by: John Scott | last post by:
I was just wondering if it's possible to cast a datagrid DataSource as a DataSet? I've stepped through my code when the grid is first being bound and the DataSource shows up as a DataSet for the...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
1
by: xrow | last post by:
Hello I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control I have problem when sorting data in datagrid I have created...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
2
by: Chris Kennedy | last post by:
I have two datagrids on a page. On the update event of the first I take some of the selected data, create a dataset and add it and then bind it to the second datagrid. If I hit update several...
2
by: Mark Rae | last post by:
I've inherited an ASP.NET app and have been asked to fix the following problem with a page which is used to display and/or delete records from a database. When the page loads, it displays a list of...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
5
by: enceladus311 | last post by:
I'm trying to find a way to keep from having to fill a DataView after every PostBack to a page. Basically, the design is that I have a DataView that I fill, which I then set as the DataSource to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.