0

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?

Chris
  • 7,415
  • 21
  • 98
  • 190

2 Answers2

5

You can set the OnClientPopulating property of the AJAX auto complete extender to a JS that clears the hidden field.

function ClearHidden(source, eventArgs) {
        document.getElementById('<%=this.hdnUsrIdSel.ClientID%>').value = null;
}

<ajaxToolkit:AutoCompleteExtender ID="autoCompleteExtend" runat="server" 
        MinimumPrefixLength="1" ServiceMethod="GetCompletionList" 
        FirstRowSelected="true" 
        ServicePath="~/WebServices/Autocomplete.asmx" TargetControlID="txtUsers" 
        UseContextKey="True" CompletionSetCount ="10" 
        OnClientPopulating="ClearHidden" 
Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
user1121070
  • 89
  • 2
  • 5
0

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);
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • I was considering that, but I thought about the possibility of them starting typing, deciding not to search then clicking a link in the screen such as a different page in a gridview - if that happened it'd say there was no account. – Chris Oct 28 '10 at 10:57
  • Just had a brainwave using your solution - if I use the onkeypress to erase the hiddenfield, then I could just set a session to become true when the users clicks the Submit button - if there is no session or it's false when the page loads, then resort back to the old data. – Chris Oct 28 '10 at 11:11
  • I wonder what happens when a user selects all and cuts using a mouse. – MIWMIB Feb 13 '17 at 07:00