Accessing server control with javascript on a webform with a master page?
I have a webform which uses a masterpage which in turn inherits from another masterpage.
I have a dropdownlist drpCursos
In my javascript code I tried to access the control but with no luck:
var combo = document.forms[0]['drpCursos'];
combo
is null
After checking the source code, I changed it to:
var combo = document.forms[0]['ctl00_ContentPlaceHolder1_drpCursos'];
It works now, but I don't think it's a good solution...
How would I do this the right way?
EDIT: My javascript
code is in an exter开发者_如何转开发nal file.
I tried Darin's solution but it didn't work, then I pasted my code directly in the aspx page and now it worked.
Also, it only works when the code is inside a function, otherwise I get undefined
Why does this happen?
var combo = document.forms[0]['<%= drpCursos.ClientID %>'];
Darin's solution will work, but if you are using .net 4.0 there are possibly better solutions now. Using the ClientIDMode property of the control you can control what names get assigned to the control at run-time (i.e. the 'ctl00_ContentPlaceHolder1_drpCursos').
i.e. <asp:Label ID="Label1" runat="server" ClientIDMode="[Mode Type]"/>
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
Excerpted below:
The Mode Types
Legacy: The default value if ClientIDMode is not set anywhere in the control hierarchy. This causes client side IDs to behave the way they did in version 2.0 (3.0 and 3.5 did not change this code path) of the framework. This mode will generate an ID similar to “ctl00_MasterPageBody_ctl01_Textbox1.”
Inherit: This is the default behavior for every control. This looks to the controls parent to get its value for ClientIDMode. You do not need to set this on every control as it is the default, this is used only when the ClientIDMode has been changed and the new desired behavior is to inherit from the controls parent.
Static: This mode does exactly what you think it would, it makes the client side ID static. Meaning that what you put for the ID is what will be used for the client side ID. Warning, this means that if a static ClientIDMode is used in a repeating control the developer is responsible for ensuring client side ID uniqueness.
Predictable: This mode is used when the framework needs to ensure uniqueness but it needs to be done so in a predictable way. The most common use for this mode is on databound controls. The framework will traverse the control hierarchy prefixing the supplied ID with it’s parent control ID until it reaches a control in the hierarchy whose ClientIDMode is defined as static. In the event that the control is placed inside a databound control a suffix with a value that identifies that instance will also be added to the supplied ID. The ClientIDRowSuffix property is used to control the value that will be used as a suffix (see samples). This mode will generate an ID similar to “Gridview1_Label1_0”
If you have just some controls where you need the ID for use with javascript, I would personally use the 'static' method, assign ID's that I want, and let .net assign the rest in the default manner.
Try this:
//default ID form asp.net is aspnetForm
// id$ = id ending with ...
var combo = $("form#aspnetForm").find("[id$='drpCursos']");
Or
var combo = $("form#aspnetForm").find("select[id$='drpCursos']");
Regards
精彩评论