简体   繁体   English

如何在asp.net中的UpdatePanel中获取控件的ID

[英]How to get id of controls within UpdatePanel in asp.net

As I'm new in asp.net development and this could be a silly question but I'm asking this question here. 由于我是asp.net开发的新手,这可能是一个愚蠢的问题,但是我在这里问这个问题。

Id of controls like button, textbox in update panel is not visible in client side debugger and if I write document.getelementbyid it gives me null value of the id of controls within updatePanel. 客户端调试器中不显示按钮,更新面板中的文本框之类的控件的ID,如果我编写document.getelementbyid,则它将为我提供updatePanel中控件ID的空值。

Actually My requirement is I want to write some javascript functions and in those functions I want to use the ids of controls. 实际上,我的要求是我想编写一些javascript函数,并且在这些函数中我想使用控件的ID。

So please guide me. 所以请指导我。

Regards, Vivek 问候,Vivek

The .net controls that have runat="server" lives in the server. 带有runat =“ server”的.net控件位于服务器中。 The id you assign to it can not be used in client side(JavaScript, Jquery ). 您为其分配的ID不能在客户端(JavaScript,Jquery)中使用。 What you can do is to assign a css class to that control and use Jquery to find that element. 您可以做的是将一个CSS类分配给该控件,然后使用Jquery查找该元素。

For example : 例如 :

<asp:Button ID="btnCopy" CssClass="btnCopy" runat="server"  Text="Copy" />


 $('.btnCopy').click(function (evt) {

                //Javascript code

        });

this will get you the on click event of the server button. 这将使您获得服务器按钮的on click事件。

You can grape the control and do what ever you want with it. 您可以掌握该控件,并随心所欲地做它。

Example : 范例:

var button =   $('.btnCopy');

// do what ever you want with the button. //使用该按钮即可完成所需的操作。

Your button control must be nested inside some parent controls.So for accessing that control you should use ClientId property of control because the ID generated in the final HTML is not guaranteed to remain the same.So it's recommended to use ClientId by considering future changes ie it might happen that the control might move in some other containers. 您的按钮控件必须嵌套在某些父控件中。因此,访问该控件时,您应使用控件的ClientId属性,因为不能保证最终HTML中生成的ID保持不变。因此,建议您考虑将来的更改使用ClientId ,例如控件可能会在其他一些容器中移动。 Refer below example: 请参考以下示例:

document.getElementById("<%=txtName.ClientID%>").value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM