Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 455251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:21:07+00:00 2026-05-12T22:21:07+00:00

I’m sure this is simple but it’s driving me nuts. I have a ListBox

  • 0

I’m sure this is simple but it’s driving me nuts.

I have a ListBox on my page to show artists, which calls a method when the index is changed, and a button which loads an artist from that list in another page when clicked:

<asp:ListBox ID="lbArtists" runat="server" Rows="1" AutoPostBack="true" OnSelectedIndexChanged="ShowArtistsWorks" />

<asp:Button ID="btnEditArtist" runat="server" Text="Edit the artist" OnClick="LoadArtist" />

Further on, I have a similar list of links, which also has an autopostback method:

<asp:ListBox ID="lbLinks" runat="server" Rows="1" AutoPostBack="true" OnSelectedIndexChanged="LoadLink" />

The problem is, when I invoke ShowArtistsWorks() by clicking btnEditArtist, the LoadLink() method also gets called. Why is that happening? Why would that get called when I haven’t changed the index on the lbLinks ListBox? It shouldn’t be going near that method.

EDIT: (relevant) Code-behind methods (

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack){
        GetArtists(); // populates artists listbox
        GetLinks(); // populates links listbox
    }
}

protected void LoadArtist(object sender, EventArgs e){
    if (lbArtists.SelectedValue != "")
        Response.Redirect("Artist.aspx?id=" + lbArtists.SelectedValue);
}

protected void LoadLink(object sender, EventArgs e)
{
    if (lbLinks.SelectedValue != "")
        Response.Redirect("Link.aspx?id=" + lbLinks.SelectedValue);
}

EDIT #2: I could easily kludge a fix for this in the individual methods to stop them happening when they shouldn’t, but I want to understand why methods that I don’t call, and that only get called from one place, get invoked inadvertently.

ACCEPTED ANSWER: Even though Boon (now CRice) got in first with an explanation and a solution, I decided to accept Jeff’s more thorough explanation because that was what I wanted, a more in-depth analysis. Thanks to everyone who answered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:21:07+00:00Added an answer on May 12, 2026 at 10:21 pm

    Change events are raised on every postback for which they’re relevant – as described in the MSDN topic “ASP.NET Web Server Control Event Model.”

    Change events in HTML server controls
    and Web server controls, such as the
    TextBox control, do not immediately
    cause a post. Instead, they are raised
    the next time a post occurs.

    When users click your ‘Edit Artist’ button, ASP.NET thinks lbLinks.SelectedIndex has changed, so it invokes its SelectedIndexChanged handler.

    The reason ASP.NET thinks the index has changed is this: when the page first loads, lbLinks doesn’t have a selected index (or value) unless you say otherwise by explicitly setting it. Until you do that, the selected index is -1 and its selected value is an empty string. The selected value (in this case, an empty string) is written to view state when the page is rendered so that ASP.NET can tell if the value has changed on postbacks.

    You can observe this while debugging by inspecting your list boxes’ selected indices and values before rendering, or you can use one of the online view state decoders (like this one) to see what’s in your page when it’s first written (though to read this, you need to know about the structure of serialized view state data).

    When you next post back, the HTML <select> element lbLinks has a non-empty value, and it is submitted as part of the post data. Take a look at Request.Form["lbLinks"] and you’ll see that it equals lbLinks.Items[0].Value.

    ASP.NET maps the posted value to lbLinks.SelectedValue, but it also knows that the selected value used to be an empty string – it gets the old value from view state. Since the two values are different, the process raises the control’s selected index changed event, causing the undesirable behavior you’ve observed.

    As boon suggested, the solution is to always explicitly set the SelectedIndex for all your ListBox controls when you’re using the OnSelectedIndexChanged event, even if you’re just setting the index to zero.

    (The AutoPostBack setting is an unrelated red herring. If you remove it from both list boxes, their OnSelectedIndexChanged events will both fire every time you click the button.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.