Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
For an example I have 2 columns Country and States in gridview.
I am using the auto complete Ajax control to select value for country column’s text box in grid view.
I need the respective country’s selected value in state column’s text box.


I am not able to access the text box selected value through Java script.

XML
<script language="javascript" type="text/javascript">
    function ForStateValue(source, eventArgs)
{

  var ValueID = "<%= GridView1.TemplateControl.FindControl("txtState").ClientID %>";
    document.getElementById(ValueID).value = eventArgs.get_value();
    var textID = "<%= GridView1.TemplateControl.FindControl("txtCountry").ClientID %>";
    document.getElementById(textID).value = eventArgs.get_text();
}
</script>




XML
<asp:TemplateField HeaderText="Country">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Justify" VerticalAlign="Middle" />
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" ></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry"
ServicePath="AutoComplete.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1"
EnableCaching="true"  OnClientItemSelected="ForStateValue"/>
</ItemTemplate>
</asp:TemplateField>
Posted
Updated 10-Mar-11 21:20pm
v3
Comments
Dalek Dave 11-Mar-11 3:20am    
Edited for Readability.

1 solution

You cannot use 'TemplateControl.FindControl()'
You'd rather be using GridView1.Rows[x].FindControl().
ASP.Net assigns a different id for text box in each row. You may have to loop thru each for its id.
You could do that on the server side with a loop.

But the only way to do it in javascript is to check the id of the textbox in the generated html & follow it
In your case the textboxes may be named like
GridView1_ctl02_txtState (for the first row) and it increments to ..ctl03.., ..ctl04.. & so on. The id will be different if you're keeping the gridview in another container.

Check the id & loop in javascript
Eg:

var i = 2;
var txt;
while(1)
{
 txt =document.getElementById('GridView1_ctl0' + i.toString() + '_txtState'); 
 if(txt!=null)
 {
   // process your textbox
 }
 else
 {
   break; // all rows completed
 }
}


And if the control id goes beyond ctl09, you must remove the '0' (must continue with ctl10 not ctl010). A little tweak required there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900