DropDownList DataBind无法与UpdatePanel一起使用?

时间:2018-09-21 03:39:09

标签: c# asp.net

我尝试更新ddlBuilding,但未显示任何内容。 ddlBuilding和ddlBuilding也什么也没显示。(没有UpdatePanel可以正常显示。)

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
    <div class="form-group  col-md-12">
        <label for="name">PCCC</label>
        <asp:DropDownList ID="ddlDistrict" runat="server" CssClass="form-control select2 select2-hidden-accessible" OnSelectedIndexChanged="ddlDistrict_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList>
    </div>
    <div class="form-group  col-md-12">
        <label for="name">Building</label>
        <asp:DropDownList ID="ddlBuilding" runat="server" class="form-control select2 select2-hidden-accessible">
        </asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>

代码后面。

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
            FillDistrict();
    }

}
void FillDistrict()
{
    DataTable district = Instant.GetAllDistrict();
    ddlDistrict.DataSource = district;
    ddlDistrict.DataValueField = "id";
    ddlDistrict.DataTextField = "district_name";
    ddlDistrict.DataBind();
}

protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable buildingz = Instant.GetBuildingByDistrictID(int.Parse(ddlDistrict.SelectedValue));
    ddlBuilding.DataSource = buildingz;
    ddlBuilding.DataValueField = "id";
    ddlBuilding.DataTextField = "building_name";
    ddlBuilding.DataBind();
}

sr我的英语不好

2 个答案:

答案 0 :(得分:0)

您需要在标记的“更新面板”中添加触发器 之后

<ContentTemplate>
 .....
</ContentTemplate>

<Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlDistrict" 
                EventName="SelectedIndexChanged" />
        </Triggers>

答案 1 :(得分:0)

您需要更新更新面板。

因此,在您的下拉列表选择索引更改方法中编写以下代码

protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable buildingz = Instant.GetBuildingByDistrictID(int.Parse(ddlDistrict.SelectedValue));
    ddlBuilding.DataSource = buildingz;
    ddlBuilding.DataValueField = "id";
    ddlBuilding.DataTextField = "building_name";
    ddlBuilding.DataBind();
    UpdatePanel1.Update()
}

因为您设置了UpdateMode =“ Conditional”,所以必须从代码中进行更新。