views:

124

answers:

3

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;
    }
+1  A: 

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.

Niike2
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.
asmo
+1  A: 

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.

Jeroen
Then how can I access the content of my DataList whenever the user click on a button?
asmo
Depends on what you want to do. Can you explain more of what you want to achieve?
Jeroen
Each DataItem contains the properties of a field in my form. I need to access the DataItem after the user clicked on the submit button in order to get these properties.For example, they allow me to know if a field is required or not, if a field needs to get its content validated somehow, etc.
asmo
If I don't rebind the fields on postback, all my asp:PlaceHolder get empty. I use them in order to dynamically choose the control that will be displayed for each field in the form.
asmo
Read my updated answer.
Jeroen
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.
asmo
So what are you putting in those placeholders?
Jeroen
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.
asmo
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.
Jeroen
This won't work, since the DataItem isn't be accessible on postback.
asmo
A: 

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.

asmo
Good luck. You'll need it.
Jeroen
Well I don't see any other choice. And it is working pretty well so far.
asmo