基于查询字符串自动填充文本框

时间:2013-10-23 15:54:23

标签: c# asp.net ajax autocomplete

我使用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();
     }                    
   }             
 }

我没有遇到任何错误,只是没有从查询字符串中存储'site'。我已经在我的页面的其他区域使用了它,它工作正常。

我希望它工作的另一种方式是使用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语句。

任何人都可以帮忙解决这个问题。我真的很感激。 非常感谢

1 个答案:

答案 0 :(得分:0)

比在webmethod中获取querystring参数更好的方法是将参数作为参数传递给webmethod:

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetRoomList(string prefixText, int count, string site)
{
    ...
}

在调用页面上,您必须在Page_Load函数中将查询字符串设置为contextKey。 contextKey可以转移到web方法,以便您可以在web方法中访问此变量。