【问题标题】:Populating AutoComplete from WebService从 WebService 填充 AutoComplete
【发布时间】:2013-03-18 18:04:59
【问题描述】:

我已经设法使用 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,这怎么可能?

【问题讨论】:

  • 这个问题我没看懂,能详细点吗?你的意思是如何在webservice响应中访问data.d
  • 好吧,我的数据阅读器会从数据库中输出球队名称,但我需要访问球队的 ID 号才能将它们添加到联赛表中
  • 您是否有理由通过类分配它而不是使用 id 开始?它会暗示您有多个要绑定的文本框,但我只看到需要一个。如果是 id 导致问题,因为 asp 正在向 id 添加东西,请使用 ClientIDMode="static"。

标签: c# javascript asp.net json web-services


【解决方案1】:

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

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

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

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

我会选择选项 3...

【讨论】:

    【解决方案2】:

    希望 like 能更好地满足您的需求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2011-12-29
      • 2012-04-28
      • 2013-07-31
      相关资源
      最近更新 更多