Turns out my previous solution was a trip down an evil rabbit hole due to reusing a global variable from my JavaScript in my code behind (doh). Here is the corrected solution, which is a bit uglier: First, in the custom control I made a public property for the listbox ClientID within the control I wished to access in my Javascript:
public partial class Controls_Locations : System.Web.UI.UserControl
{
public ListBox Locations
{
get {return lbControlVehicles.ClientID; }
}
Then in my page code behind I provided a public property for this listbox ClientID string:
private Controls_Locations userLocations;
public string UserVehiclesListBox
{
get { return userLocations.Locations; }
}
In my Page_Load I initialize userLocations:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
userLocations = this._Locations1;// _Locations1 is the custom control in my aspx file which contains the listbox I want to access
In my aspx file using Javascript:
<uc1:RastracUserVehicle_Locations ID="RastracUserVehicle_Locations1" runat="server" />
<script type="text/javascript" language="javascript">
var myLocationList;
function getLocationListBox()
{
myLocationList = $get("<%=UserVehiclesListBox%>");
//myLocationList is now the listbox contained within the custom control
}
I used a "global" Javascript variable so that I only need to get the list once. I have not yet discovered whether accessing the list will cause a post back to the server everytime.