【发布时间】:2026-01-10 11:10:01
【问题描述】:
我有一个按钮,当按下它时,会根据列表框选择查找值,如果它在表中找到记录,它会从列表框中获取该值并将其放入会话中,刷新页面,然后会话用作数据源,即。找到会话 = 会话的位置。 现在发生的情况是,如果我想进行两次连续搜索,该按钮不会存储新会话,而是使用旧会话。因此,如果我先搜索 x,然后搜索 y,它会在页面刷新时添加 x。
protected void search(object sender, EventArgs e)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM Driver WHERE City = '" + JourOrigin.SelectedItem + "' ";//retrieves driver names from table
dr = cmd.ExecuteReader();
dr.Read();
if(Session["city"] != null)
{
Session["city"] = null;
JourOrigin.SelectedValue = null;
}
else
{
if(dr.HasRows)
{
Session["city"] = JourOrigin.SelectedItem.ToString();
Response.Redirect("~/Account/FindDriver.aspx");
NoCity.Visible = false;
}
else
{
DriversJourney.Items.Clear();
DriversJourney.Items.Add("No Drivers in selected city, try another city");
NoCity.Visible = true;
NoCity.Text = "No drivers in selected city, please try another city";
}
}
con.Close();
}
如果已经有会话,我设法清除会话,但我必须按两次 value 来存储它。每次按下按钮时是否可以“刷新”会话?
【问题讨论】:
-
在您的代码
if(Session["city"] != null)中,您也清除了收入值。从此 IF 中删除JourOrigin.SelectedValue = null;。其次,会话存储在 ELSE 中,因此如果会话不为空,您的代码将删除会话并且不要执行代码的另一步骤。更改您的 if 并删除 else。让代码在 IF 之后执行。 -
我需要else,因为一旦我点击按钮会话将清除,它不会检查会话是否为空