views:

35

answers:

1

I have a page that has a few textboxes and a dropdownlist. When a logged in user opens this page the boxes are filled with data that he has inputed before, he can change the data and update it by pushing a update button. There is also that dropdownlist which has a dynamically populated data for him to choose from.

What is the best way to make this page work. Where in the page cycles do i populate the forms and where do i input the data to the datatable.

At the moment I'm populating the data on PreRender but the dropdownlist on the preinit. I have a button event handler to do the update on the datatable. The problem is that the autopostback fucks up the data in the dropdownlist because its dynamatically populated, how would i go by fixing this ?

+3  A: 

Here is what I follow in most of my cases

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        InitDropdDownListes();
        LoadDataFromDataBase();
    }

}

void InitDropdDownListes()
{
    // fill drop down boxes

}

void LoadDataFromDataBase()
{
    // load from database

}

protected void OnDropdownListChanges(object sender, EventArgs e)
{
    // reload the new data from database
    LoadDataFromDataBase();
}


protected void btnSave_Click(object sender, EventArgs e)
{
    // Save to database

}
Aristos
Oh, the memories... Fifteen hundred methods that all start with `if(!IsPostBack)`.
Will
@Will - and as soon as I read this question it all came flooding back. One man's memories is another man's nightmare (ie. the person having to maintain the old ASP.NET codebase :)
Todd Smith
but here you populate the dropdownlist only if its not a postback, when i push the save button it posts back and then there are no items in the dropdownlists. dynamatically populated data doesnt stay there after postback..
eski
@eski I will check it, but I think that they saved on viewstate - do you have enable the viewstate for the DropDownListBox ? Maybe you need to populate by hand on function, I will check out to see what I do.
Aristos
@Aristos yes the viewstate is enabled. I'm populating the dropdownlist by doing linq query and binding it to the datasource on it. Maybe i need to add each item with a foreach loop to the list ?
eski
Ahhhh.. *feelingstupid* I'm using masterpages and the viewstate on the content placeholder was false. The input boxes and dropdownlist were in that placeholder. ;)
eski