Why are calls made when the Asp.Net ListBox auto-retry method is called?

I'm sure it is simple, but it drives me crazy.

I have a ListBox on my page to show artists who call the method when the index changes, and a button that loads the artist from this list on 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" />

In addition, I have a similar list of links, which also has an auto-repeat method:

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

The problem is that when invoked ShowArtistsWorks()by pressing btnEditArtist, the method is also called LoadLink(). Why is this happening? Why is this caused when I did not change the index in the lbLinksListBox? He should not come close to this method.

EDIT: (relevant) Code-Code 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);
}

№ 2: , , , , , , , .

: , ( CRice) , , , , . , .

+5
7

, - MSDN - ASP.NET ".

HTML- -, TextBox, . .

" ", ASP.NET , lbLinks.SelectedIndex , SelectedIndexChanged.

, ASP.NET , , : lbLinks ( ), , . , -1, . ( ) , , ASP.NET , .

, - ( ), , , ( ).

, HTML <select> lbLinks . Request.Form["lbLinks"], , lbLinks.Items[0].Value.

ASP.NET lbLinks.SelectedValue, , - . , , , .

, SelectedIndex ListBox, OnSelectedIndexChanged .

( AutoPostBack - . , OnSelectedIndexChanged .)

+2

. Index -1, , , 0, , - , . , , , .

, . :

if (!IsPostBack){  
    GetArtists(); // populates artists listbox  
    GetLinks(); // populates links listbox

    lbArtists.SelectedIndex = 0;
    lbLinks.SelectedIndex = 0;
} 
+1

, ? , , post-pack?

0

, LoadLinks() Page_Load() . , , . . , .

0

, .

, , Index , .

, .

0

AutoPostBack = "true". , - , AutoPostBack = "true", .

EDIT. , - , , SelectedIndexChanged true .

0

My thought is that the second list does not start with the selected item. When the page loads, your browser automatically selects the first item. When you publish a page, the view state has changed (there is no item for the first item), so a postback event occurs. Try to make sure you have a choice.

0
source

All Articles