【问题标题】:asp:DropDownList datasource is null after submit when declared inside an asp:UpdatePanel在 asp:UpdatePanel 中声明时,asp:DropDownList 数据源在提交后为空
【发布时间】:2016-04-13 04:40:27
【问题描述】:

我在 UpdatePanel 中使用 DropDownList。当用户在 TextBox 中写入一些文本并单击“Go”按钮时,它会填充。

xaml 如下:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:TextBox ID="ODSearchTB" runat="server" ClientIDMode="Static"></asp:TextBox>
        <asp:Button ID="ODFindButton" 
            class="btn btn-default goButton"
            runat="server" Text="Go" 
            ClientIDMode="Static" 
            UseSubmitBehavior="False" 
            CausesValidation="False" 
            OnClick="ODFindButton_Click" /><br/>
        <asp:DropDownList ID="ODSearchDDL" runat="server" Visible="False"  AutoPostBack="True"></asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>

以及点击后填充 DropDownList 的代码:

protected void ODFindButton_Click(object sender, EventArgs e)
{
    var res = ServiceUnitOfWork.MedicalOffices.FindMedicalOfficesByName(
        ODSearchTB.Text,
        doctor: false,
        hospital: true,
        service: false,
        establishment: true);
    List<LightMedicalOffice> source = getLightMedicalOffices(res.ToList());

    ODSearchDDL.DataSource = source;
    ODSearchDDL.DataTextField = "Name";
    ODSearchDDL.DataValueField = "IdAddress";
    ODSearchDDL.DataBind();
    ODSearchDDL.Visible = true;
}

这很好用,数据源将正确更新,并且 DropDownList 将被填充和更新(在 UpdatePanel 中),而无需重新加载页面。提交表单时出现问题:Page_Load 事件中的 DropDownList 为空(数据源 null),因此当我到达 Submit_ClickCustomValidator_ServerValidate 事件时,我不知道选择的值是什么列表。

页面和控件上似乎都启用了 ViewState。我不知道是什么导致了这种行为。

【问题讨论】:

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


    【解决方案1】:

    您是否在 Page_Load 事件中重置 DropDown?我在上面实现了您的代码,并在更新面板之外添加了一个提交按钮。我能够在提交按钮的 OnClick 事件中捕获 DropDown 控件的值。

    【讨论】:

    • 不,我没有。 I4ve 尝试使用 var val = dropdownlist.DataSource 捕获 Page_Load 第一行的值,并且在提交后此时它已经为空。
    • 你为什么要捕获数据源?如果您正在捕获值怎么办:ODSearchDDL.SelectedValue.
    • 好吧,我已经尝试捕获 dropDownList 的任何属性,它是空的,并且 selectedvalue 是“”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2014-01-24
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多