How DropDownList's SelectedIndexChanged() works without PostBack?

34,783

Solution 1

You could put the DropDownList into an <asp:UpdatePanel> and set the trigger to the SelectedIndexChanged event of the DropDownList.

Something like this (don't forget the script manager)

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
      <asp:DropDownList ID="drop1" runat="server" OnSelectedIndexChanged="ddlTablo_SelectedIndexChanged" />
   </ContentTemplate>
   <Triggers>
      <asp:AsyncPostbackTrigger ControlID="drop1" EventName="SelectedIndexChanged" />
   </Triggers>
</asp:UpdatePanel>

Solution 2

You can send ajax call, using asp.net UpdatePanel or use jQuery ajax. This wont do postback and your whole page wont get refreshed.

The UpdatePanel is quite straight forward and easy to use. ASP.net ajax will generate the asyn calls for you whereas jQuery ajax will probably need you to render html using javascript.

Solution 3

In the code snippet below, add this parameter: AppendDataBoundItems="True"

<asp:DropDownList ID="ddlGroupNameFilter" 
    runat="server" 
    AutoPostBack="true" 
    AppendDataBoundItems="true" 
    OnSelectedIndexChanged="ddlLeadgroupName_SelectedIndexChange">
</asp:DropDownList>
Share:
34,783
Jude
Author by

Jude

.NET Developer SOreadytohelp

Updated on January 18, 2020

Comments

  • Jude
    Jude over 4 years

    DropDownList's SelectedIndexChanged() Event fills the ListBox on the page. Obviously this posts the page back to the server. Is there any way to make it happen without full postback?

    protected void ddlTablo_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<string> list = new List<string>();
        ListBox1.Items.Clear();
        var columnNames= from t in typeof(Person).GetProperties() select t.Name;
        foreach (var item in columnNames)
        {
             list.Add(item);
        }
        ListBox1.DataSource = list;
        ListBox.DataBind();
    }
    
  • Adil
    Adil over 10 years
    Any kind of ajax (async call) could be used.
  • Green_qaue
    Green_qaue over 9 years
    Hi, can you please explain what difference the "AsyncPostbackTrigger" does? To me it looks like I get the same effect without it? Since I'm setting the first dropdown to AutoPostBack="true". (Im updating a second DDL after what is chosen in the first)
  • slfan
    slfan over 9 years
    You can see an answer to your question here: stackoverflow.com/questions/15330089/…. Basically a sync postback will post and load the whole form whereas an asyncpostback only reloads part of the page.
  • Max Voisard
    Max Voisard over 4 years
    @slfan You say to set AutoPostBack="true" on the DropDownList but don't do it in the example. Which is right?
  • slfan
    slfan over 4 years
    @MaxVoisard. You are right, the AutoPostBack is not required, because we have the AsyncPostbackTrigger. I updated my answer