vivek jain
vivek jain

Reputation: 591

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.

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.

Actually My requirement is I want to write some javascript functions and in those functions I want to use the ids of controls.

So please guide me.

Regards, Vivek

Upvotes: 0

Views: 4344

Answers (2)

Amol Kadam
Amol Kadam

Reputation: 85

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 i.e. it might happen that the control might move in some other containers. Refer below example:

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

Upvotes: 0

user123456
user123456

Reputation: 2659

The .net controls that have runat="server" lives in the server. The id you assign to it can not be used in client side(JavaScript, Jquery ). What you can do is to assign a css class to that control and use Jquery to find that element.

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.

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.

Upvotes: 1

Related Questions