Autopostback and Listbox

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.
 
J

James Thomas

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
 
T

Tackie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top