如何使用Javascript代码在Gridview中访问文本框值 [英] How to access textbox values inside Gridview using Javascript code

查看:107
本文介绍了如何使用Javascript代码在Gridview中访问文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在gridview中有2列Country(国家)和States(州).
我正在使用自动完成Ajax控件在网格视图中为国家/地区"列的文本框选择值.
我需要在国家/地区列的文本框中选择相应国家/地区的选定值.


我无法通过Java脚本访问文本框选择的值.

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.

<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>









<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>

推荐答案

您不能使用''TemplateControl.FindControl()''
您宁愿使用GridView1.Rows [x] .FindControl().
ASP.Net在每一行中为文本框分配一个不同的ID.您可能需要遍历每个ID.
您可以在服务器端执行循环操作.

但是在javascript中执行此操作的唯一方法是在生成的html&中检查文本框的ID.跟随
在您的情况下,文本框可能命名为
GridView1_ctl02_txtState(用于第一行),并递增到..ctl03 ..,.. ctl04 ..&很快.如果将gridview保留在另一个容器中,则ID将有所不同.

检查ID&在javascript中循环
例如:

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
 }
}



并且,如果控件ID超出了ctl09,则必须删除"0"(必须继续输入ctl10而不是ctl010).在那里需要一些调整.



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


这篇关于如何使用Javascript代码在Gridview中访问文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆