ASP.NET / DataItem of DataList is null after Postback
Asked Answered
O

3

9

After postback (click on a button) in my ASP.NET form, all the DataItem of my form are null. Why? What should I do to retrieve the content of the DataList even after postback?

protected void buttonAddRecord_Click(object sender, EventArgs e)
    {
        foreach (DataListItem item in listFields.Items)
        {
            // item.DataItem == null  WTF?
        }
    }

protected void Page_Load(object sender, EventArgs e)
    {
        BindFields();
    }

private void BindFields()
    {
        object setting = MySettings.GetSetting();

        if (!Null.IsNull(setting))
        {
            listFields.DataSource =     
                DataProvider.GetData(int.Parse(setting.ToString()));
            listFields.DataBind();
        }

        listFields.Visible = listFields.Items.Count > 0;
        emptyMessage.Visible = listFields.Items.Count == 0;
    }
Oudh answered 20/8, 2010 at 13:57 Comment(0)
O
9

Found my answer here.

What John said, the data source items are only avaliable when databound. They are no longer accessable after initial loading.

You might consider having an object or object collection representing onscreen data that you update with the grid, then persist changes from that to databases.

More precisely, I used an HiddenField to store an ID across posts and I request data from the database instead of trying to get it form the DataItem (which can't be used outside the databinding event).

The HiddenField control is used to store a value that needs to be persisted across posts to the server.

Oudh answered 20/8, 2010 at 15:58 Comment(4)
Well I don't see any other choice. And it is working pretty well so far.Oudh
HiddenField worked for me. I'm using a repeater and need to persist data from binding to button clicks.Strigose
This sounds like a good way to make twice as many database calls as needed. Seems like DataList might have a poor model.Simar
That link is expired.Nightspot
B
6

DataItem is only available when databinding.

Load comes before Click so you're overwriting your data anyways.

Do this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindFields();
    }
}

You should use a DataSource (like ObjectDataSource) to handle DataBinding and Update/Insert.

Update / advise:

Using PlaceHolders to bind data to you are getting yourself in trouble. You should consider using either a ListView, GridView, DataList or Repeater. I'm sure any of those do what you want and you will have to program less. Use your time to learn them instead of trying to get this to work, its doomed to fail.

Bijugate answered 20/8, 2010 at 14:12 Comment(5)
I'm already using a DataList (check out the title). And I'm forced to use PlaceHolder since I can't know in advance what will be the Control type of each field. Actually, each DataListItem of my DataList contains one PlaceHolder control which type will be determined in upon data binding.Oudh
So what are you putting in those placeholders?Bijugate
Each placeholder contains one control, such as TextBox, CheckBox, RadioButtonList, DropDownList, etc. I can't know the exact control type since its dynamic. Each placeholder represents a field in a form, for example, Name, Address, Email, Favorite Food, etc.Oudh
you have to add them on postback, but dont put the data. Add a boolean parameter to your binding function, set it to false when postbacking. when its true you add the control + data if its false you only add the control and extract the data on click.Bijugate
This won't work, since the DataItem isn't be accessible on postback.Oudh
L
0

Check if you really DataBind() the DataList after each postback. Normally you get DataList, GridView, DropDownList (and other Controls) empty after a PostBack when you don't bind them again.

Lon answered 20/8, 2010 at 14:8 Comment(1)
Thanks for the hint, but I really do rebind the DataList even on postback. I also checked in the debugger, and the DataSource get bound on postback.Oudh

© 2022 - 2024 — McMap. All rights reserved.