模式弹出AJAX在刷新时退出
本文关键字:刷新 退出 AJAX 模式 | 更新日期: 2023-09-27 18:22:23
这是一个情况,我有一个AJAX模式弹出窗口,在我的面板内是两(2)个连接的下拉列表。一个代表欧洲大陆,另一个代表各国。一个例子是,当用户选择亚洲时,国家的下拉列表中应该有数据。这是我的模式弹出窗口和面板的代码
<asp:ModalPopupExtender ID="Modalpopupextender1" runat="server" TargetControlID="ShowPopUpButton"
PopupControlID="pnlpopup" CancelControlID="CancelButton" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="pnlpopup" runat="server" BackColor="White" Height="269px" Width="400px"
OnLoad="pnlpopup_Load">
<tr>
<td align="right">
Continent:
</td>
<td>
<asp:DropDownList ID="ContinentDownList" runat="server"
onselectedindexchanged="ContinentDropDownList_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right">
Country:
</td>
<td>
<asp:DropDownList ID="CountryDropDownList" runat="server">
</asp:DropDownList>
</td>
</tr>
</asp:Panel>
现在我的问题是,当我的模式弹出窗口加载时,当我选择一个大陆时,国家的下拉列表不会加载。当我在ContinentDropDown中插入AutoPostBack="true"时,模式弹出窗口会刷新并退出。我花了很长时间调试并知道如何解决这个问题。救命
这是后面的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadContinent();
LoadCountry();
}
}
public void LoadContinent()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand com = new SqlCommand("Reader.usp_LoadContinentDropDownList", con))
{
com.CommandType = CommandType.StoredProcedure;
con.Open();
try
{
SqlDataReader dr = com.ExecuteReader();
OwnerGroupDropDownList.DataSource = dr;
OwnerGroupDropDownList.DataTextField = "fld_Description";
OwnerGroupDropDownList.DataValueField = "fld_ContinentID";
ContinentDropDownList.DataBind();
}
catch (SqlException)
{
Response.Write("<script>alert('The database has encountered an error. Please try again')</script>"); }
catch (Exception)
{
Response.Write("<script>alert('The database has encountered an error. Please try again')</script>"); }
}
}
LoadContinentDropDownList.Items.Insert(0, new ListItem("<Select Person Group>", "0"));
}
public void LoadCountry()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand com = new SqlCommand("Reader.usp_LoadCountryDropDownList", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@fld_ContinentId", SqlDbType.Int));
com.Parameters["@fld_ContinentId"].Value = ContinentDropDownList.SelectedValue;
con.Open();
try
{
SqlDataReader dr = com.ExecuteReader();
OwnerDropDownList.DataSource = dr;
OwnerDropDownList.DataTextField = "fld_Description";
OwnerDropDownList.DataValueField = "fld_CountryID";
CountryDownList.DataBind();
}
catch (SqlException)
{
Response.Write("<script>alert('The database has encountered an error. Please try again')</script>"); }
catch (Exception)
{
Response.Write("<script>alert('The database has encountered an error. Please try again')</script>"); }
}
}
CountryDropDownList.Items.Insert(0, new ListItem("<Select Person>", "0"));
}
protected void ContinentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadContinent();
LoadCountry();
}
是否尝试将pnlPopup
面板放入ASP.NET UpdatePanel
控件中?现在,当下拉列表进行回发时,它将是部分回发,因为它们都在UpdatePanel
中,并且应该保持弹出面板上的可见性。
有关创建和使用UpdatePanel
的更多信息,请参阅UpdatePanel控件简介。
当您将控件放在UpdatePanel
之外时,AutoPostback="true"
会导致完全回发,将弹出面板"重置"为隐藏,因为这是模式弹出扩展程序目标控件的默认状态。