Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to access a ListBox within a custom control with Javascript in the aspx page.

How to access a ListBox within a custom control with Javascript in the aspx page.

Scheduled Pinned Locked Moved ASP.NET
javascriptdesignsysadmintoolstutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rico MNC
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups