请问如下这是为什么呢?如何解决?

.aspx文件中的部分代码如下:
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="205px"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" Height="109px" Width="209px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

.aspx.cs文件的部分代码如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{fun1();
fun2();
//两个函数都是访问后台数据库
}
问题是:当我选择下拉框时,整个页面任然在刷新。用debug看后,发现DropDownList1_SelectedIndexChanged函数执行完后,将继续执行Page_Load函数。请问这是为什么呀?怎么解决呢?注:我用的是vs2008。
听取以上回答的意见后,我更改了一些代码,如下:
.aspx文件:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="DSort1" runat="server" AutoPostBack="True" Width="205px" OnSelectedIndexChanged="DSort1_SelectedIndexChanged">
</asp:DropDownList><asp:ListBox ID="LSort2" runat="server" Height="109px" width="209px"></asp:ListBox></ContentTemplate><Triggers><asp:AsyncPostBackTrigger ControlID="DSort1" EventName="SelectedIndexChanged" /></Triggers></asp:UpdatePanel> 
.cs文件:
Page_Load事件加上 if(!IsPostBack),它和DSort1_SelectedIndexChanged函数里面的函数都是doWorks_getSort1();doWorks_getSort2();(从数据库中读数据到列表框中)。但是,改变下拉框的选项时,整个页面任然在刷新,debug测试发现,执行完DSort1_SelectedIndexChanged函数后,将会执行Page_UnLoad函数。

不负相思意
浏览 84回答 3
3回答

BIG阳

AutoPostBack="True"&nbsp;自动回发给服务器 每次触发selectedIndexChanged事件 就会把相关数据传给服务 服务器处理完selectedIndexChanged事件后, 重新生成页面传给客户端,故会执行page_load你可以在page_load事件中判断是不是回发处理页面 if (!ispostback){这里面的代码只在页面第一次加载时执行}

慕仙森

在if(!IsPostBack)里后加上doWorks_getSort1();doWorks_getSort2();后再加上response.end()试试.这样的话就会关闭流输出.我也没试过,凭感觉的

RISEBY

几个问题:第一,感觉你没有必要用两个UpdatePanel第二,Triggers只对外面的控件有效,因为你外面的控件在另一个UpdatePane里面,所以它找不到UpdatePane里面的控件第三,Triggers只有当UpdatePane的UpdateMode属性设置为Conditional时才有效,你改成以下试试
打开App,查看更多内容
随时随地看视频慕课网APP