如何获取放置在gridview行中的服务器控件的客户端ID [英] How to get Client id of server control placed in gridview row

查看:83
本文介绍了如何获取放置在gridview行中的服务器控件的客户端ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我动态地在gridview中创建行,在获取要在JavaScript中使用的服务器控件(文本框)的clientid时遇到问题.

看一下代码以了解问题.

错误:名称"txt_date"在当前上下文中不存在

符合以下条件:

Hi
I am creating row in gridview dynamicaly, having problem in geting clientid of server control (textbox) to use in javascript.

have a look at the code to understand prblem.

error : The name ''txt_date'' does not exist in the current context

in line :

document.getElementById('<%= txt_date.ClientID %>'),'mm/dd/yyyy',this

                       <asp:TemplateField HeaderText="Date">
                                    <ItemTemplate>
                                        <div style="float:left;">
                                            <asp:TextBox ID="txt_date" runat="server"  CssClass="textbox"></asp:TextBox>
                                            <img alt="Calender" id="img" height="16" src="images/calender.jpg" width="16" 

                                          oncick="displayCalendar( document.getElementById('<%= txt_date.ClientID %>'),'mm/dd/yyyy',this);" />
                                          </div>
                                       <div style="float:left; margin:4px;">
                                        </div>
                                        <div style="float:left;">
                                            <asp:CustomValidator ID="CustomValidator2" runat="server" 

                                                ErrorMessage="* Invalid Date" OnServerValidate="custDate_ServerValidate"></asp:CustomValidator>
                                        </div>
                                    </ItemTemplate>
                                    <ItemStyle Width="25%" />
                                </asp:TemplateField>

推荐答案

使用此功能:

Use this function :

function GetClientId(strid)
{
     var count=document.getElementsByTagName ('*').length; //<-- gets all elements, instead of Forms as this only returns FORM elements
     var i=0;
     var eleName;
     for (i=0; i < count; i++ )
     {
       eleName=document.getElementsByTagName ('*')[i].id;
       pos=eleName.indexOf(strid);
       if(pos>=0)  break;
     }
    return eleName;
}



传递txt_date作为输入,您将获得相同的clientId.您可以将断点保留在该函数的最后一行,并评估eleName.

有时,为服务器端控件(尤其是对于用户控件,数据网格元素)找到client ID变得非常麻烦.在这种情况下,您可以部署此功能以找到有关服务器控件的client ID.只需controlServer Side Name,您将获得相同的客户端ID.

希望这会有所帮助.
一切顺利.



Pass txt_date as input to it, you will get the clientId for the same. You can keep the Breakpoint on the last line of this function and evaluate eleName.

Sometimes, it becomes very troublesome to find the client ID for the server side controls, especially for user controls, data grid elements. In such scenarios you can deploy this function to find the client ID for the server control in question. Just the Server Side Name of the control and you will get the client ID for the same.

Hope this helps.
All the best.


在JS中,您可以得到服务器控件,例如

document.getElementById(''<%= txt_date.ClientID%>'')
In JS you can get the server control like

document.getElementById(''<%=txt_date.ClientID%>'')


为什么
看这个
http://sanjaysainitech.blogspot.com/2008/07/get-find- functions-in-javascript.html [ ^ ]
hy
look at this
http://sanjaysainitech.blogspot.com/2008/07/get-find-functions-in-javascript.html[^]


这篇关于如何获取放置在gridview行中的服务器控件的客户端ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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