我使用 ajax 创建了一个自动完成文本框,效果很好。它基本上提供了房间代码列表。这是代码:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetRoomList(string prefixText, int count)
{
using (SqlConnection roomsConnection = new SqlConnection())
{
roomsConnection.ConnectionString = ConfigurationManager.ConnectionStrings["roomsConnectionString"].ConnectionString;
using (SqlCommand roomsCommand = new SqlCommand())
{
roomsCommand.CommandText = "select Room, Site from tblRoom where Room like '%' + @SearchText + '%'";
roomsCommand.Parameters.AddWithValue("@SearchText", prefixText);
roomsCommand.Connection = roomsConnection;
roomsConnection.Open();
List<string> roomList = new List<string>();
using (SqlDataReader roomReader = roomsCommand.ExecuteReader())
{
while (roomReader.Read())
{
roomList.Add(roomReader["Room"].ToString());
}
}
roomsConnection.Close();
return roomList;
}
}
}
但是,我希望它根据用户在上一页中选择的选项生成此房间代码列表。有两种方法可以传递选项。一种是使用查询字符串。这是我写的代码:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetRoomList(string prefixText, int count)
{
using (SqlConnection roomsConnection = new SqlConnection())
{
roomsConnection.ConnectionString = ConfigurationManager.ConnectionStrings["roomsConnectionString"].ConnectionString;
HttpRequest request = HttpContext.Current.Request;
string site1 = request.QueryString["site"];
using (SqlCommand roomsCommand = new SqlCommand())
{
roomsCommand.CommandText = "select Room, Site from tblRoom where Site = '" + site1 + "' AND Room like '%' + @SearchText + '%'";
roomsCommand.Parameters.AddWithValue("@SearchText", prefixText);
roomsCommand.Connection = roomsConnection;
roomsConnection.Open();
List<string> roomList = new List<string>();
using (SqlDataReader roomReader = roomsCommand.ExecuteReader())
{
while (roomReader.Read())
{
roomList.Add(roomReader["Room"].ToString());
}
}
return roomList;
roomsConnection.Close();
}
}
}
我没有收到任何错误,只是没有从查询字符串中存储“站点”。我已经在我页面的其他区域使用了它,它工作正常。
我希望它工作的另一种方式是使用 Page.PreviousPage。这又是我写的:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetRoomList(string prefixText, int count)
{
using (SqlConnection roomsConnection = new SqlConnection())
{
roomsConnection.ConnectionString = ConfigurationManager.ConnectionStrings["roomsConnectionString"].ConnectionString;
if (Page.PreviousPage != null)
{
if (Page.PreviousPage.IsCrossPagePostBack == true)
{
string site1 = PreviousPage.getDropDownListSite.SelectedValue;
using (SqlCommand roomsCommand = new SqlCommand())
{
roomsCommand.CommandText = "select Room, Site from tblRoom where Site = '" + site1 + "' AND Room like '%' + @SearchText + '%'";
roomsCommand.Parameters.AddWithValue("@SearchText", prefixText);
roomsCommand.Connection = roomsConnection;
roomsConnection.Open();
List<string> roomList = new List<string>();
using (SqlDataReader roomReader = roomsCommand.ExecuteReader())
{
while (roomReader.Read())
{
roomList.Add(roomReader["Room"].ToString());
}
}
return roomList;
roomsConnection.Close();
}
}
}
}
}
我根本无法让这段代码工作,因为它不喜欢 Page.PreviousPage。我收到错误
An object reference is required for the non-static filed, method, or property 'system.Web.UI.Page.PreviousPage.get'
但我不知道还能用什么。我尝试使用一个效果很好的会话变量,但如果我用户需要在同一个会话中再次使用该表单,它会保留最后一个值。
一旦我让各个组件工作,我打算给我们一个 if,else 语句。
有人能帮忙吗。我真的很感激。非常感谢