Autopostback and Listbox

  • Thread starter Thread starter Tackie
  • Start date Start date
T

Tackie

Okay guys, I'll admit I'm new to asp.net but this one is shouldn't be that
difficult to fix.

I have a listbox which is populated and works fine. I have added code the
SelectedIndexChanged Event to populate a couple of textboxes with data.
Both the populating the listbox and retrieving data work fine.

Here is my problem:

Each time the SelectedIndexChanged Event is called the currently selected
item in my list box is lost. Meaning after each postback the page_load
event is fired and the index is set back to 0.

Any insight would be greatly appreciated.
 
You need to put the code that loads your list box in Page_Load like
this:

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
// Code to populate listbox goes here
}
}

This way, the code to populate the list box will only be called the
first time a page loads - afterwards, all subsequent loads are post
back so it won't run the 2nd time.

James
 
Worked like a charm, thanks James!

James Thomas said:
You need to put the code that loads your list box in Page_Load like
this:

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
// Code to populate listbox goes here
}
}

This way, the code to populate the list box will only be called the
first time a page loads - afterwards, all subsequent loads are post
back so it won't run the 2nd time.

James
 
Back
Top