Question

I would like my AutoCompleteExtender selection to fire an event in code behind. I've fired onClick events from javascript code before, but that seems like extra steps in this case. Ultimately when the user selects a name from the AutoCompleteExtender list, I want to take the ID associated with that name, call a service to get more data, then add to a GridView on the page. What I have so far...

<ajaxToolkit:AutoCompleteExtender ID="StudentNameSearchTextBox_AutoCompleteExtender" runat="server"
    TargetControlID="StudentNameSearchTextBox" 
    ServicePath="~/Scripts/AutoComplete.asmx"
    ServiceMethod="GetStudents"
    OnClientItemSelected="onStudentSelected"
    MinimumPrefixLength="2" 
    CompletionSetCount="30" 
    UseContextKey="True" 
    CompletionListCssClass="autocomplete_completionListElement" 
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" 
    CompletionListItemCssClass="autocomplete_listItem" >
</ajaxToolkit:AutoCompleteExtender>

The javascript:

<script type="text/javascript">
    function onStudentSelected(sender, e) {
        var selectedStudent = eval("(" + e._value + ")");
        alert(selectedStudent); //this alerts what I expect
        //how to call the SingleStudentSelected event, and send the selectedStudent 

with?

    }
</script>

Code behind:

    protected void SingleStudentSelected(object sender, EventArgs e)
    {
        ///TODO: get data from service and add to grid
    } 

Can I do this directly, or do I need to have a hidden button or textbox to fire an OnChange event? I would like to accomplish this the cleanest way possible. Thanks.

Update- This is the target textbox--

<asp:TextBox ID="StudentNameSearchTextBox" runat="server" 
    CssClass="StudentNameSearch" OnTextChanged="StudentNameSearchTextBox_TextChanged"
    ViewStateMode="Inherit"></asp:TextBox>

Added this to the javascript:

    __doPostBack(sender.get_element().name, selectedStudent);

And this is the event:

    protected void StudentNameSearchTextBox_TextChanged(object sender, EventArgs e)
    {
        ///TODO: get data from service and add to grid
    }   

So, I need a hidden field to hold the selectedStudent (VALUE of the selected item) so I can use it in the StudentNameSearchTextBox_TextChanged event?

Was it helpful?

Solution

In my opinion the cleanest way is to save selected value to HiddenField's value, fire PostBack from hidden button and do all staff in button's click event handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top