正在从WebService填充自动完成功能

Populating AutoComplete from WebService

本文关键字:成功 功能 填充 WebService      更新时间:2023-09-26

我已经使用Web服务在C#.net中实现了JQuery自动完成。

这是asp代码:

    <div class="row">
    <div class="span4">
        <h3>
            Manage Season</h3>
    </div>
</div>
<div class="row">
    <div class="span2">
        <p>
            <label class="control-label" for="TeamName">
                Team Name:</label></p>
    </div>
    <div class="span3">
        <asp:TextBox ID="TeamNameTextBox" runat="server" CssClass="searchinput"></asp:TextBox>
        <asp:Button ID="AddTeamButton" CssClass="btn btn-primary" runat="server" Text="Add"
            OnClick="AddTeamButton_Click" />
    </div>
<script type="text/javascript">
    $(document).ready(function () {
        $(".searchinput").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "PredictiveSearch.asmx/GetAllPredictions",
                    data: "{'keywordStartsWith':'" + request.term + "'}",
                    dataType: "json",
                    async: true,
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Due to unexpected errors we were unable to load data");
                    }
                });
            },
            minLength: 1
        });
    });
</script>

以及c#网络服务:

    [System.Web.Script.Services.ScriptService]
public class PredictiveSearch : System.Web.Services.WebService
{
    [WebMethod]
    public IList<string> GetAllPredictions(string keywordStartsWith)
    {
        //TODO: implement real search here!
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("[dbo].[findEnglishTeams]", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        string searchTerm = keywordStartsWith;
        SqlParameter searchTermParam = new SqlParameter("@searchterm", searchTerm);
        cmd.Parameters.Add(searchTermParam);
        IList<string> output = new List<string>();
        conn.Open();
        SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (dReader.HasRows)
        {
            while (dReader.Read())
            {
                output.Add(dReader["englishTeamName"].ToString());
            }
            return output;
        }
        else
        {
            return output; 
        }
    }
}

我需要获得我在下拉列表中填充的值的ID,这怎么可能?

由于您在客户端使用Ajax请求填充此内容,因此您必须:

  1. 通过将所选值写入一个html输入type=hidden元素,并在表单返回时在服务器端读取它来获取所选值。只是不要忘记通过添加runat="server"使input type=hidden元素成为服务器端控件
  2. 通过另一个Ajax请求提交所选的值
  3. 使用Request.Params集合读取选定的值,并使用列表框的名称作为键。类似于:

    var selectedValues = Request.Params["select_box_name"];
    

您将无法简单地使用ListBox.SelectedValue,因为在ViewState中找不到值,因为您是通过Ajax填充它的。

我会选择选项3…

希望这样的东西能更好地满足您的需求