【发布时间】: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_Selecting和Page_Load的顺序可以改吗?
除了DropDownListYear.Items.Insert(0, new ListItem("Please select year", "0")) 强制用户选择年份之外,我还能如何处理?
【问题讨论】:
标签: c# webforms .net-4.0 dropdown objectdatasource