Click here to Skip to main content
15,943,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having issue while passing the client id value of asp.net control getting changed in JavaScript. I am getting exception while passing the value of a dropdown (ctl00_cphDDS_drdInstructionCode) as updated below.
In one server it is rendering as ctl00_cphDDS_drdInstructionCode but in another server it is rendering as cphDDS_drdInstructionCode. Application is running in asp.net with 4.6.2 framework. I want to resolve this issue globally. Sample code is updated below.

What I have tried:

Code is as below:
JavaScript
var DropdownList = document.getElementById("ctl00_cphDDS_drdInstructionCode");
var SelectedIndex = DropdownList.selectedIndex;
var SelectedValue = DropdownList.options[DropdownList.selectedIndex].value;
var txtComments = document.getElementById("ctl00_cphDDS_txtUserComments").value;
Posted
Updated 25-Jan-24 3:28am
v4

1 solution

You can use the ClientIDMode="Static" attribute on the control (DropDownList), which ensures that the client ID remains the same as the server ID.
ASP.NET
<asp:DropDownList runat="server" ID="drdInstructionCode" ClientIDMode="Static">
    <!-- Your rest of code -->
</asp:DropDownList>
This approach should help you avoid issues related to varying client IDs in different server environments. Also see the link to explore this feature
https://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900