【发布时间】:2015-09-24 14:52:38
【问题描述】:
我想在选择 ListBox 的 SelectedIndexChanged 事件时从数据库中加载记录。我没有在我的表单中使用更新面板控件,一切都很好。现在我想使用更新面板控件,这样每次我从列表框中选择一个项目时,我的表单都不会回发。我试过了,但没有按预期工作。请帮助我克服我面临的这个问题。
.aspx 代码
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:ListBox ID="lstTest" runat="server" AutoPostBack="True" Width="450px" OnSelectedIndexChanged="lstTest_SelectedIndexChanged"></asp:ListBox>
<asp:TextBox ID="txtReport" runat="server"></asp:TextBox>
<asp:TextBox ID="txtImression" runat="server"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lstTest" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
.cs 后面的代码
protected void lstTest_SelectedIndexChanged(object sender, EventArgs e)
{
int _serviceid = Convert.ToInt32(lstTest.SelectedValue);
txtReport.Text = string.Empty;
txtImression.Text = string.Empty;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[get_rad_results]";
cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = _serviceid;
cmd.Connection = con;
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtReport.Text = dr["Report"].ToString();
txtImression.Text = dr["Impression"].ToString();
}
}
catch (Exception ex)
{
Response.Write("Error occured: " + ex.Message.ToString());
}
finally
{
con.Close();
con.Dispose();
}
}
【问题讨论】:
-
使用 PostBackTrigger 而不是 AsyncPostBackTrigger。
-
您的页面是否有任何脚本错误?你能验证你的
lstTest_SelectedIndexChanged(..)被执行了吗?我不太确定但不会按预期工作的确切含义