Jude
Jude

Reputation: 2433

How to fire a DropDownList's SelectedIndexChanged() event without full 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();
}

Upvotes: 6

Views: 38309

Answers (3)

slfan
slfan

Reputation: 9129

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>

Upvotes: 9

Vinoth Dexterity
Vinoth Dexterity

Reputation: 21

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>

Upvotes: 2

Adil
Adil

Reputation: 148140

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.

Upvotes: 3

Related Questions