我需要相互加载下拉列表。我做了如下:
<asp:DropDownList ID="drblLanguages" runat="server" OnSelectedIndexChanged="drblLanguages_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList ID="DrpCategory" runat="server" OnSelectedIndexChanged="DrpCategory_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
</asp:DropDownList>
<asp:DropDownList ID="DrpEmailTemplate" runat="server" Enabled="false" OnSelectedIndexChanged="DrpEmailTemplate_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
我的后端代码如下:
protected void drblLanguages_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
LoadDataForCategoty();
}
catch (Exception ex)
{
SendToErrorPage(ex);
}
}
protected void DrpCategory_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
LoadEmailTemplateToDropDown();
}
catch (Exception ex)
{
SendToErrorPage(ex);
}
}
在Page_load事件中
protected override void OnInit(EventArgs e)
{
try
{
LoadLanguageDropdowns();
}
catch (Exception ex)
{
SendToErrorPage(ex);
}
}
在此处语言更改时会加载相关类别。但是当类别被更改时,它总是选择第一个记录(索引-1)并且它调用drblLanguages_SelectedIndexChanged
事件而不是DrpCategory_SelectedIndexChanged
。似乎下拉从头再次初始化。我该如何解决这个问题?
答案 0 :(得分:1)
if(!IsPostBack)
{
LoadLanguageDropdowns();
}
你试试这个吗?您应该在Page_Load中加载下拉列表,而不是在OnInit中。这将仅在首次打开时加载您的下拉列表,并且不应出现您的问题。
编辑:您有三个下拉列表
<asp:DropDownList runat="server" ID="LanguageDdl" OnSelectedIndexChanged="LanguageDdl_SelectedIndexChanged" DataTextField="Title" DataValueField="ID" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="CategoryDdl" OnSelectedIndexChanged="CategoryDdl_SelectedIndexChanged" DataTextField="Title" DataValueField="ID" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="TemplateDdl" OnSelectedIndexChanged="TemplateDdl_SelectedIndexChanged" DataTextField="Title" DataValueField="ID" AutoPostBack="true">
</asp:DropDownList>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LanguageDdl.DataSource = new Varmebaronen.AppCode.BO.Object().GetList();
LanguageDdl.DataBind();
}
}
protected void LanguageDdl_SelectedIndexChanged(object sender, EventArgs e)
{
//if query the database for dataSource like in my case here you can
// take value of the languagedropdown and fetch the category for the current language
CategoryDdl.DataSource = new Varmebaronen.AppCode.BO.Category().GetList();
CategoryDdl.DataBind();
}
protected void CategoryDdl_SelectedIndexChanged(object sender, EventArgs e)
{
TemplateDdl.DataSource = new Varmebaronen.AppCode.BO.Product().GetList();
TemplateDdl.DataBind();
}
protected void TemplateDdl_SelectedIndexChanged(object sender, EventArgs e)
{
}