tomfogarty
asked on
Can't access textbox for Autocomplete in repeater
Hi,
I'm trying to access a textbox in a repeater using a small javascript script. It's called by an ajax autocomplete when the user chooses an option from the list.
It's not working because the javascript can't access the textbox (ID=ContactID). The reason is because it's in a repeater. So how do I modify the script to access that particular textbox in the repeater?
Tom.
I'm trying to access a textbox in a repeater using a small javascript script. It's called by an ajax autocomplete when the user chooses an option from the list.
It's not working because the javascript can't access the textbox (ID=ContactID). The reason is because it's in a repeater. So how do I modify the script to access that particular textbox in the repeater?
Tom.
<script type="text/javascript" >
function OnContactSelected(source, eventArgs) {
$get('<%# ContactID.ClientID %>').value = eventArgs.get_value();
}
</script>
Is the textbox incluced in each item or just a single item say header?
ASKER
There will be one in each row. The context is that the contactid is the name of a person that's assigned to something else and the user can add multiple so there could me **many** of them.
When is this OnContactSelected method called and what is the source parameter?
ASKER
It's called when an item on the list is selected. Specified in the ASP.NET code.
http://www.asp.net/ajaxLib rary/AjaxC ontrolTool kitSampleS ite/AutoCo mplete/Aut oComplete. aspx
http://www.asp.net/ajaxLib
ASKER
Sorry- should have specified this.
We set the Javascript as a tag parameter of the ASP.NET
Full example for choosing a company...
We set the Javascript as a tag parameter of the ASP.NET
onclientitemselected="OnCompanySelected"
Full example for choosing a company...
<asp:autocompleteextender runat="server" id="AutoComplete" servicemethod="GetCompanyList" targetcontrolid="CompanyInputBox" completionlistcssclass="autotypeahead_completionListElement" onclientitemselected="OnCompanySelected" minimumprefixlength="2" completioninterval="200" completionlistitemcssclass="autotypeahead_listItem" completionlisthighlighteditemcssclass="autotypeahead_highlightedListItem"></asp:autocompleteextender>
Ok I see now...so you want to assign the selected value to some other field correct? I take ContactID is that other control.
If you can post the markup of your repeater, it would help.
If you can post the markup of your repeater, it would help.
ASKER
Here is the code for the repeater only. You'll see the ContactID textbox and the autocomplete controls below also.
<asp:Repeater runat="server" id="CompanyContactTableControlRepeater"> <ITEMTEMPLATE> <ICBEMS:CompanyContactTableControlRow runat="server" id="CompanyContactTableControlRow">
<tr><td class="tableCellSelectCheckbox" scope="row" style="font-size: 5px;" colspan="2">
<asp:CheckBox runat="server" id="CompanyContactRecordRowSelection" onclick="moveToThisTableRow(this);"> </asp:CheckBox><br /><br />
</td><td class="tableRowButton"><asp:ImageButton runat="server" id="CompanyContactRowDeleteButton" causesvalidation="False" commandargument="DeleteOnUpdate" commandname="Custom" cssclass="button_link" imageurl="../Images/icon_delete.gif" onmouseout="this.src='../Images/icon_delete.gif'" onmouseover="this.src='../Images/icon_delete_over.gif'" tooltip="<%# GetResourceValue("Txt:DeleteRecord", "ICBEMS") %>">
</asp:ImageButton></td><td class="tableRowButton"><asp:ImageButton runat="server" id="CompanyContactRowEditButton" causesvalidation="False" commandname="Redirect" cssclass="button_link" imageurl="../Images/icon_edit.gif" onmouseout="this.src='../Images/icon_edit.gif'" onmouseover="this.src='../Images/icon_edit_over.gif'" tooltip="<%# GetResourceValue("Txt:EditRecord", "ICBEMS") %>">
</asp:ImageButton></td><td class="tableCellValue" style="white-space:nowrap;"><div style="display:none"><span style="white-space:nowrap;">
<asp:TextBox runat="server" id="ContactID" Columns="40" MaxLength="40" cssclass="field_input" minlistitems="Default"></asp:TextBox>
<BaseClasses:TextBoxMaxLengthValidator runat="server" id="ContactIDTextBoxMaxLengthValidator" ControlToValidate="ContactID" ErrorMessage="<%# GetResourceValue("Val:ValueTooLong", "ICBEMS").Replace("{FieldName}", "Contact") %>"></BaseClasses:TextBoxMaxLengthValidator></span>
</div>
<div style="position:relative;"><asp:textbox id="ContactAutoCompleteTarget" runat="server"></asp:textbox></div>
<asp:autocompleteextender runat="server" id="ContactAutoComplete" targetcontrolid="ContactAutoCompleteTarget" servicemethod="GetContactList" completionlistcssclass="autotypeahead_completionListElement" onclientitemselected="OnContactSelected" minimumprefixlength="2" completioninterval="200" completionlistitemcssclass="autotypeahead_listItem" completionlisthighlighteditemcssclass="autotypeahead_highlightedListItem"></asp:autocompleteextender></td><td class="tableCellValue" style="white-space:nowrap;"></td><td class="tableCellValue" style="white-space:nowrap;"><asp:ImageButton runat="server" id="ContactIDAddRecordLink" causesvalidation="False" commandname="Redirect" imageurl="../Images/iconNewFlat.gif" tooltip="<%# GetResourceValue("Btn:Add", "ICBEMS") %>">
</asp:ImageButton></td></tr><tr><td class="tableRowDivider" colspan="5"> </td><td class="tableRowDivider"></td><td class="tableRowDivider"></td></tr></ICBEMS:CompanyContactTableControlRow>
</ITEMTEMPLATE>
</asp:Repeater>
ok then here's one way you can try:
function OnContactSelected(source, eventArgs) {
var tid=source.get_element(); //grab the associated textbox
var contact = $(tid).parent().find("input[id$='_ContactID']"); //find the contactid textbox
contact.val(tid.value);
alert(contact.val());
}
ASKER
Getting an error.
Make sure you have added reference to jquery in the <head> section of your page.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
ASKER
I'm getting a messagebox saying "undefined".
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That's giving me a message box with the Text of the selected item of the auto complete. The ID of the ID complete is ContactAutoCompleteTarget. I need to get the value (an ID) of that item and write it into the ContactID textbox. Getting there...
Sorry I didn't get that last part. Didn't you want to add the selected item to the contactID?
Not sure how you are getting ID as the autocomplete will return a string[ ] only correct?
Not sure how you are getting ID as the autocomplete will return a string[ ] only correct?
ASKER
It's OK - this line finishes it.
Now the autocomplete works.
Thanks a million.
contact.val(eventArgs.get_value());
Now the autocomplete works.
Thanks a million.