Zdarzenie SelectedIndexChanged z listy DropDownList nie jest uruchamiane

Mam obiekt DropDownList na mojej stronie internetowej. Kiedy klikam na niego i wybieram inną wartość, nic się nie dzieje, mimo że mam funkcję podłączoną do zdarzenia SelectedIndexChanged.

Po pierwsze, rzeczywisty kod HTML obiektu:

<asp:DropDownList ID="logList" runat="server" 
       onselectedindexchanged="itemSelected">
</asp:DropDownList>

I to jest ta funkcja, itemSelected:

protected void itemSelected(object sender, EventArgs e)
{
    Response.Write("Getting clicked; " + sender.GetType().ToString());
    FileInfo selectedfile;
    Response.Write("<script>alert('Hello')</script>");
    foreach (FileInfo file in logs)
    {
        if (file.Name == logList.Items[logList.SelectedIndex].Text)
        {
            Response.Write("<script>alert('Hello')</script>");
        }
    }
}

Żadna z odpowiedzi nie pojawia się, a ta część JavaScript nigdy nie jest uruchamiana. Próbowałem tego w najnowszej wersji 3.6 Firefoksa, a także Internet Explorer 8. To jest serwowane z komputera z systemem Windows Server 2003 R2, działającego ASP.NET z. NET Framework w wersji 4.

Author: TylerH, 2011-02-05

7 answers

Ustaw właściwość DropDownList AutoPostBack na true.

Eg:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>
 412
Author: Vyasdev Meledath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-05 06:06:51

Spróbuj ustawić AutoPostBack="True" Na DropDownList.

 80
Author: The Scrum Meister,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-05 06:05:22

Znam jego nieco starszy post, ale mimo to chciałbym dodać coś do powyższych odpowiedzi.

Może zaistnieć sytuacja, w której " wartość " więcej niż jednego elementu z listy rozwijanej jest zduplikowana / taka sama. Dlatego upewnij się, że nie ma powtarzanych wartości w elementach listy, aby wywołać zdarzenie " onselectedindexchanged "

 45
Author: 4u.Ans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-13 04:09:15

Dodaj Właściwość ViewStateMode="Enabled" i EnableViewState="true" And AutoPostBack="true" in drop DropDownList

 12
Author: Dilip Kr Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-14 08:03:44

Upewnij się również, że strona jest poprawna. Możesz to sprawdzić w przeglądarkach developer tools (F12)

W zakładce Console wybierz właściwy cel / ramkę i sprawdź właściwość [Page_IsValid]

Jeśli strona nie jest poprawna, formularz nie zostanie przesłany i w związku z tym nie odpali zdarzenia.

 4
Author: HerbalMart,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-24 12:04:41

Dla mnie odpowiedzią był atrybut strony aspx, dodałem async = "true" do atrybutów strony i to rozwiązało mój problem.

<%@ Page Language="C#" MasterPageFile="~/MasterPage/Reports.Master"..... 
    AutoEventWireup="true" Async="true" %>

Oto struktura mojego panelu aktualizacji

<div>
  <asp:UpdatePanel ID="updt" runat="server">
    <ContentTemplate>

      <asp:DropDownList ID="id" runat="server" AutoPostBack="true"        onselectedindexchanged="your server side function" />

   </ContentTemplate>
  </asp:UpdatePanel>
</div>
 3
Author: Justin F,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-26 17:45:45

Zamiast tego, co napisałeś, możesz zapisać go bezpośrednio w zdarzeniu SelectedIndexChanged kontrolki dropdownlist, np.

protected void ddlleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
 //code goes here
}
 1
Author: user2541273,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-03-12 16:16:51