我被一个问题困住了,我真的没想到会被耽搁这么多小时,这让我发疯
如标题中所述,我需要两个 DropDownLists,其中 ToYears 列表的值从 FromYear 的选择开始:
FromYear
2000
2001
2002
2003
2004
2002
年
2003
年 2004 年
所以我尝试使用 2 < asp:dropdownlist > 并在 FromYear 的 SelectedIndexEvent 期间更改选定的 ToYear ,但这会触发选定的 FromYear 事件,并且它不会再次触发。
现在我从 Ajax Control Toolkit 中找到了CascadingDropDown,并认为这可能是件好事。但我不想调用网络服务,而是想在实际页面后面的代码中使用方法。
回发后还应记住选择 - 年份范围根据后面代码中的属性而变化。
我在某处读到自动回发不适用于 CascadingDropDown。
您认为最优雅、最简单的解决方案是什么?
非常感谢您提前。
编辑:我将发布我的帖子的一些部分 - 希望对标记有所帮助:
<asp:DropDownList AutoPostBack="True" ID="DropDownFromYear" runat="server" OnSelectedIndexChanged="FromYearChanged" />
<asp:Label ID="UntilLabel" runat="server" Text=" until " />
<asp:UpdatePanel ID="ToYearUpdatePanel" runat="server" style="display: inline-block;">
<ContentTemplate>
<asp:DropDownList AutoPostBack="true" ID="DropDownToYear" runat="server" OnSelectedIndexChanged="ToYearChanged" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownFromYear" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
CodeBehind(从 OnInit 调用):
private void InitializeDropDownYears()
{
//Calculate the YearMin YearMax Properties
CalculateYearMinMax();
int adaptedFromYear = 0, adaptedToYear = 0;
//get the previously selected Years
if (DropDownToYear.SelectedItem != null) adaptedToYear = int.Parse(DropDownToYear.SelectedValue);
if (DropDownFromYear.SelectedItem != null) adaptedFromYear = int.Parse(DropDownFromYear.SelectedValue);
//check the minimum year constraints 2005 was selected but minYear is 2010 -> adpated is set to 2010
if (YearMin > adaptedFromYear || adaptedFromYear == 0) adaptedFromYear = YearMin;
if (YearMax < adaptedToYear || adaptedToYear == 0) adaptedToYear = YearMax;
//check the 5 year range constraint
if ((YearMax - YearMin) > 5)
{
adaptedFromYear = DateTime.Now.Year - 2;
adaptedToYear = DateTime.Now.Year + 2;
}
Dictionary<string, string> toYears = new Dictionary<string, string>();
Dictionary<string, string> fromYears = new Dictionary<string, string>();
for (int tempYear = YearMin; tempYear <= YearMax; tempYear++)
{
fromYears.Add(tempYear.ToString(), tempYear.ToString());
if (tempYear >= adaptedFromYear)
{
toYears.Add(tempYear.ToString(), tempYear.ToString());
}
}
DropDownFromYear.DataSource = fromYears;
DropDownFromYear.DataValueField = "Key";
DropDownFromYear.DataTextField = "Value";
DropDownFromYear.SelectedValue = adaptedFromYear.ToString();
DropDownFromYear.DataBind();
DropDownToYear.DataSource = toYears;
DropDownToYear.DataValueField = "Key";
DropDownToYear.DataTextField = "Value";
DropDownToYear.SelectedValue = adaptedToYear.ToString();
DropDownToYear.DataBind();
if(!IsPostBack)
{
SelectedFromYear = adaptedFromYear;
SelectedToYear = adaptedToYear;
}
}
private void CalculateYearMinMax()
{
IList<Task> taskList = CurrentLicense.TaskList;
List<DateTime> startDates = taskList.Select(task => task.StartDate).ToList();
YearMin = startDates.Min(date => date).Year;
List<DateTime> endDates = taskList.Select(task => task.EndDate).ToList();
YearMax = endDates.Max(date => date).Year;
}
事件处理程序:
protected void FromYearChanged(object sender, EventArgs e)
{
SelectedFromYear = int.Parse(DropDownToYear.SelectedValue);
SelectedToYear = int.Parse(DropDownFromYear.SelectedValue);
if (SelectedFromYear > SelectedToYear)
{
SelectedToYear = SelectedFromYear;
}
UpdateGanttTables();
}
protected void ToYearChanged(object sender, EventArgs e)
{
SelectedFromYear = int.Parse(DropDownToYear.SelectedValue);
SelectedToYear = int.Parse(DropDownFromYear.SelectedValue);
UpdateGanttTables();
}