DropDownList的SelectedIndexChanged()如何在没有PostBack的情况下工作?

时间:2014-02-25 18:02:51

标签: asp.net drop-down-menu postback

DropDownList的SelectedIndexChanged()事件填充页面上的ListBox。显然,这会将页面发布回服务器。有没有办法让它在没有完全回发的情况下发生?

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();
}

3 个答案:

答案 0 :(得分:8)

您可以将DropDownList放入<asp:UpdatePanel>并在DropDownList上设置AutoPostBack="true"。您必须将触发器设置为SelectedIndexChanged事件。

像这样(不要忘记脚本管理器)

<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>

答案 1 :(得分:3)

您可以使用asp.net UpdatePanel发送ajax调用或使用jQuery ajax。这不会回发,你的整个页面都不会刷新。

UpdatePanel非常简单易用。 ASP.net ajax将为您生成asyn调用,而jQuery ajax可能需要您使用javascript渲染html。

答案 2 :(得分:1)

在下面的代码段中,添加以下参数:AppendDataBoundItems="True"

<asp:DropDownList ID="ddlGroupNameFilter" 
    runat="server" 
    AutoPostBack="true" 
    AppendDataBoundItems="true" 
    OnSelectedIndexChanged="ddlLeadgroupName_SelectedIndexChange">
</asp:DropDownList>