我已经设法使用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,这怎么可能?
答案 0 :(得分:0)
由于您使用Ajax请求在客户端上填充此内容,因此您将不得不:
input type=hidden
runat="server"
元素成为服务器端控件
使用列表框的名称作为密钥,使用Request.Params集合读取所选值。类似的东西:
var selectedValues = Request.Params["select_box_name"];
您将无法简单地使用ListBox.SelectedValue
,因为在ViewState
中找不到值,因为您通过Ajax填充它。
我选择选项3 ......
答案 1 :(得分:0)
希望like能更好地满足您的需求