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.