我試圖將HiddenField值傳遞給WebMethod GetAutoCompleteData
以基於選定的搜索字段在文本框中啓用自動完成功能。傳遞隱藏域值的問題
我試過使用代碼隱藏傳遞HiddenField值,但它沒有奏效。
JavaScript代碼沒有問題。
注意:我試圖在另一種方法中使用HiddenField值,它工作,所以我確信HiddenField正在使用JavaScript代碼取值。
代碼隱藏:
public static string hdnvalue { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
hdnvalue = hdnSearchParam.Value;
}
[WebMethod]
public static List<string> GetAutoCompleteData(string value)
{
string hiddenfiedlvalue = hdnvalue;
List<string> result = new List<string>();
string strConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("select @hiddenfiedlvalue from Users where @hiddenfiedlvalue LIKE '%'[email protected]+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", value);
cmd.Parameters.AddWithValue("@hiddenfiedlvalue", hiddenfiedlvalue);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}", hiddenfiedlvalue));
}
return result;
}
}
}
代碼用於從下拉菜單中選擇的值傳遞到HiddenField:
<script type="text/javascript">
$(document).ready(function (e) {
$('.search-panel .dropdown-menu').find('a').click(function (e) {
e.preventDefault();
var param = $(this).attr("href").replace("#", "");
var concept = $(this).text();
$('.search-panel span#search_concept').text(concept);
$('[id$=hdnSearchParam]').val(param);
});
});
</script>
自動完成代碼:
<script type="text/javascript">
$(function() {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'value':'" + $('#txtSearch').val() + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert("Error");
}
});
},
});
}
</script>
ASPX:
<asp:HiddenField ID="hdnSearchParam" runat="server" />
<div class="col-xs-8">
<div class="input-group">
<div class="input-group-btn search-panel">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span id="search_concept">Filter by</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="#UserID">User ID</a></li>
<li><a href="#TypeName">User Type</a></li>
<li><a href="#UserName">User Name</a></li>
<li><a href="#FirstName">First Name</a></li>
<li><a href="#LastName">Last Name</a></li>
<li><a href="#Email">Email</a></li>
</ul>
</div>
<input type="hidden" name="search_param" value="all" id="search_param">
<asp:TextBox ID="txtSearch" CssClass="autosuggest form-control" runat="server"></asp:TextBox>
<span class="input-group-btn"></span>
</div>
</div>
你是否試圖將隱藏文本「hdnSearchParam」的值傳遞給Web方法「GetAutoCompleteData」? –
是的,這正是我的意思 – Ayman
爲什麼你在你的下拉列表中使用href? –