weixin_33695450 2010-10-28 10:39 采纳率: 0%
浏览 37

AutoCompleteExtender问题

I'm using an AutoCompleteExtender in ASP.NET/C# to retrieve data from my database, along with the primary key of the field. When a name is chosen, the details (name/pk) are retrieved even before clicking submit, and it then passes these onto a hidden field.

The issue I have is that if the user types in an incorrect name, the pk won't reset and will remain the same from the previous search - meaning that when the user clicks search, the old data will be displayed.

Here is my AutoComplete service:

 public string[] GetAutoComplete(string prefixText, int count)
    {

        string connection = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        string sql = "SELECT * FROM SageAccount WHERE Name LIKE @prefixText AND Customer = 1 AND SageID IS NOT NULL";
        SqlDataAdapter da = new SqlDataAdapter(sql, connection);
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> Names = new List<string>();
        foreach (DataRow dr in dt.Rows)
        {
            Names.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dr["Name"].ToString() + " (" + dr["SageID"].ToString() + ")", dr["ID"].ToString()));
        }

        return Names.ToArray();

    }

And the JavaScript used for populating the hiddenfield is:

function autoCompleteItemSelected(source, eventArgs) {
            var assocHiddenField = document.getElementById(source.get_id() + '_hidden');
            assocHiddenField.value = eventArgs.get_value();
        }

What is the best way to reset the hiddenfield if no results are returned? I do currently have a 'part working' solution, which is this bit of JavaScript:

   function pageLoad() {

    $find('txtName')._onMethodComplete = function(result, context) {


        $find('txtName')._update(context, result, false);
        webservice_callback(result, context);
    };


}
function webservice_callback(result, context) {
    var hiddenfield = document.getElementById('txtName_hidden');
    if (result == "")
        hiddenfield.value = '0';
}

But if the user hits enter/clicks submit very quickly, it doesn't reset. If they don't click for a second or two it works, and resets the hiddenfield to 0.

Any other ideas guys?

  • 写回答

2条回答 默认 最新

  • weixin_33726943 2010-10-28 10:46
    关注

    Try using $addHandler to attach to the keypress event of the text box. Clear your hidden field in that event handler:

    $addHandler($get("txtName"), "keypress", function(e) {
        $get("txtName_hidden").value = "";
    }, true);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!