Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DropDownList's SelectedIndexChanged() works without PostBack?

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();
}
like image 516
Jude Avatar asked Feb 25 '14 18:02

Jude


3 Answers

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>
like image 166
slfan Avatar answered Sep 21 '22 23:09

slfan


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.

like image 36
Adil Avatar answered Sep 19 '22 23:09

Adil


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>
like image 23
Vinoth Dexterity Avatar answered Sep 20 '22 23:09

Vinoth Dexterity