下拉列表未在部分回发中填充 [英] Drop Down List not getting populated on partial post back
问题描述
我正在尝试在部分回发中填充一个下拉列表,不确定为什么它不起作用.
I am trying to populate a drop down list on partial post back, not sure why it's not working.
这有效,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populatemyDropDownList();
}
这不起作用
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
populatemyDropDownList();
}
场景
* 我单击UpdatePanel_1中的button_1,然后触发部分回发(不刷新页面),并尝试填充UpdatePanel_2 *中的DropDownList
当我调试时,我可以看到方法背后的代码正在触发并经历此代码,但没有任何收益,我认为部分回发会重置DropDownList吗????
when I debug, I can see code behind method is triggering and going through this code but no gain, I think partial post back resets DropDownList ????
using (SqlDataSource sqlds = new SqlDataSource(ConnectionString(), SelectCommand()))
{
drop1.DataSource = sqlds;
drop1.DataTextField = "UserName";
drop1.DataBind();
}
推荐答案
您可以使用
ScriptManager.GetCurrent(Page).IsInAsyncPostBack
检查您是否处于异步回发中.
to check if you're in an asynchronous postback.
但是,我不会将您的逻辑依赖于回发(或!IsPostBack
)和IsInAsyncPostBack
.相反,我会使用正确的事件.在这种情况下,您要处理button_1
的click事件以将DropDownList
填充到UpdatePanel2
中.
However, i would not rely your logic on postbacks(or !IsPostBack
) and IsInAsyncPostBack
. Instead i would use the correct events. In this case you want to handle button_1
's click event to fill the DropDownList
in UpdatePanel2
.
请注意,您应该将UpdatePanel2
的UpdateMode设置为Conditional
.然后,您可以在填写DropDownList
后手动调用UpdatePanel2.Update()
.
Note that you should make UpdatePanel2
's UpdateMode Conditional
. Then you can call UpdatePanel2.Update()
manually after you've filled the DropDownList
.
这篇关于下拉列表未在部分回发中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!