AjaxPro局部刷新(输入时自动提示功能)
效果图
1.前台页面
<td class="queryLeft">股票名称:</td> <td class="queryRight"> <asp:TextBox ID="txtShareName" CssClass="shareName" onkeyup="prompt(this)" runat="server" autocomplete="off"></asp:TextBox> //okeyup()按下键盘时触发。autocomplete:消除浏览器的记录
// 隐藏div <div id="prompt" style="display: none; min-width: 120px; position: absolute; top: 200px; line-height: 20px; border: 1px solid #ccc; border-top: 0px; background-color: #fff; padding: 0px 5px; z-index: 1000;"></di> </td>
1.2 js代码
//填充鼠标背景色
function setMouseEvent() { $(".hide_share").mousemove(function () { $(this).css("background-color", "#C9C9C9").siblings().css("background-color", " #fff"); }).mouseout(function () { $(this).css("background-color", " #fff") }); };
//调用后代码,局部刷新 function prompt(obj) { var str = $(obj).val(); if (str.trim() == "" || event.keyCode == 13) { $("#prompt").hide(); $("#prompt").html(""); } var str = Lcp.Share.Admin.Web.ShareMaintenace.GetAllShareList(str).value; if(str == "") { $("#prompt").hide(); return; } $("#prompt").html(str); $("#prompt").show(); setMouseEvent(); }
//填充文本框 function fullShareName(share_name) { $(".shareName").val(share_name); $("#prompt").hide()
1.3 后台代码
protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(ShareMaintenace)); //引用 ajaxPro
if (!IsPostBack) { BindData(0); } }
(注意“\”转意字符使用)
[AjaxPro.AjaxMethod] public string GetAllShareList(string shares) { string share = shares; string shareMsg = ""; if (!string.IsNullOrEmpty(share)) { IShareService service = Kernel.GetService<IShareService>(); ShareDataGetResponse scp = service.GetShareDataGetResponse(share); scp.ShareDataList.ForEach(o => { shareMsg += "<a class='hide_share' style='display:block;' href='javascript:void(0);' onclick=\"fullShareName('" + o.ShareName + "')\">" + o.ShareCode + " " + o.ShareName + "</a> "; }); } return shareMsg; }