【问题标题】:ObjectDataSource is selecting before Drop Down List is populate with dataObjectDataSource 在下拉列表填充数据之前进行选择
【发布时间】:2018-09-14 23:56:53
【问题描述】:

我在第一个 Page_Load 期间绑定了我的 DropDownList:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Function.LoopChangeControlMtText(this.Controls);

        BindProcessingDepartment();
        BindDropDownListYear();
        if (DropDownListProcessingDepartment.Items.Count > 0)
        {
            if (DropDownListYear.Items.Count > 0)
            {
                BindGridViewUploadedItemNoCommitment();
            }
        }
    }
}

private void BindDropDownListYear()
{
    MTConfig mtConfig = new MTConfig();
    DataTable mtCreatedYear = new DataTable();
    mtCreatedYear = mtConfig.getMtCreatedYear();
    if (mtCreatedYear == null)
    {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "alertFailProcessDeptBind", "alert('Failed to get " + ConfigurationManager.AppSettings["systemNameShortFormWithoutE"].Trim() + " created year.\\nPlease contact MIS.\\nContact info. at the bottom of the page.');", true);
    }
    else
    {
        DropDownListYear.DataSource = mtCreatedYear;
        DropDownListYear.DataMember = "MtCreatedYear";
        DropDownListYear.DataValueField = "MtCreatedYear";
        DropDownListYear.SelectedIndex = 0;
        DropDownListYear.DataBind();
    }
}

但我的对象数据源在下拉列表填充数据之前执行选择。第一次选择时,下面的SelectedValue 为空。

protected void objectDataSourceUploadedItemWithoutCommitment_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    try
    {
        e.InputParameters["CreatedYear"] = Convert.ToInt32(DropDownListYear.SelectedValue.ToString());
    }
    catch (Exception ex)
    {
        LogHandler.LogError(logger, ex);
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "Error occurred during objectDataSourceUploadedItemWithoutCommitment_Selecting!", true);
    }
}

objectDataSourceUploadedItemWithoutCommitment_SelectingPage_Load的顺序可以改吗?

除了DropDownListYear.Items.Insert(0, new ListItem("Please select year", "0")) 强制用户选择年份之外,我还能如何处理?

【问题讨论】:

    标签: c# webforms .net-4.0 dropdown objectdatasource


    【解决方案1】:

    您可以使用AppendDataBoundItems="True" 属性在前端完成:

    <asp:DropDownList ID="DropDownListYear" AppendDataBoundItems="True" runat="server" >
        <asp:ListItem>Please select year</asp:ListItem>
    </asp:DropDownList>
    

    这将自动附加第一个项目以及从 DataSource 填充的所有其他项目。

    【讨论】:

    • 不工作,objectDataSourceUploadedItemWithoutCommitment_Selecting 仍然发生在第一个 Page_Load 之前,因此 DropDownList 中没有数据。
    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 2011-11-04
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多