Reputation: 525
I bind my DropDownList during first Page_Load:
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();
}
}
But my object data source perform selecting before the drop down list is populate with data. The SelectedValue
below is empty during the first select.
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);
}
}
Is it possible to change the order of objectDataSourceUploadedItemWithoutCommitment_Selecting
and Page_Load
?
How else can I deal with this other than DropDownListYear.Items.Insert(0, new ListItem("Please select year", "0"))
to force user to select a year?
Upvotes: 0
Views: 252
Reputation:
You can done it on front side using AppendDataBoundItems="True"
property:
<asp:DropDownList ID="DropDownListYear" AppendDataBoundItems="True" runat="server" >
<asp:ListItem>Please select year</asp:ListItem>
</asp:DropDownList>
This will automatically append the first item with your all other items populated from DataSource.
Upvotes: 0