Developer404
Developer404

Reputation: 5982

Javascript function is not working properly in Master pages

document.getelementbyid('txtbox') is not working when I used in content page as it is working in the normal web page. The value is null when it is used in contentpage. Plz anybody help me

Upvotes: 0

Views: 710

Answers (3)

rahul
rahul

Reputation: 187110

Read this article

Control ID Naming in Content Pages

ASP.NET allows certain controls to be denoted as naming containers. A naming container serves as a new ID namespace. Any server controls that appear within the naming container have their rendered id value prefixed with the ID of the naming container control.

Naming containers not only change the rendered id attribute value, but also affect how the control may be programmatically referenced from the ASP.NET page's code-behind class. The FindControl("controlID") method is commonly used to programmatically reference a Web control. However, FindControl does not penetrate through naming containers. Consequently, you cannot directly use the Page.FindControl method to reference controls within a GridView or other naming container.

Master pages and ContentPlaceHolders are both implemented as naming containers.

Upvotes: 1

manji
manji

Reputation: 47968

When the page renders, if the textBox is under another control, the Id tends to change. You can use the ClientId property:

document.getElementById("<%= txtbox.ClientID %>")

Upvotes: 2

FinnNk
FinnNk

Reputation: 3275

The id will have changed, you can use something like:

document.getelementbyid(<%=txtTextBox.ClientID%>).value

or you can view the source to get the id in the hopes that it will not change again.

If you have the option I'd switch to some other engine, such as asp.net mvc where you have control over the HTML.

Upvotes: 2

Related Questions