Home All Groups Group Topic Archive Search About

DataGrid DataSource is null on postback

Author
24 Dec 2005 3:27 AM
Narshe
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.

Author
24 Dec 2005 8:37 AM
Riki
Do you bind it declaratively or with code, and if so, what is your code?

--

Riki

Show quote
"Narshe" <nar***@gmail.com> wrote in message
news:1135394846.184988.245020@f14g2000cwb.googlegroups.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.
>
Author
24 Dec 2005 3:26 PM
Narshe
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();
    }
}
Author
26 Dec 2005 4:29 AM
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

Show quote
"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();
>     }
> }
>
>
Author
26 Dec 2005 4:37 PM
Teemu Keiski
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


Show quote
"Narshe" <nar***@gmail.com> wrote in message
news:1135437987.772028.101900@g14g2000cwa.googlegroups.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();
> }
> }
>

AddThis Social Bookmark Button