Locate control inside User control

Last post 05-12-2008 10:57 AM by mthakershi. 4 replies.

Sort Posts:

  • Locate control inside User control

    05-09-2008, 4:03 PM
    • Loading...
    • mthakershi
    • Joined on 05-07-2007, 5:22 PM
    • Posts 53

    Hi,

    I am really confused about when to use:

    $get()

    $find()

    getElementByID()

    This becomes more confusing when there is Master page, child page and user controls inside it.

    For example: I am in a page that has a master page. There are some user controls of certain type inside the child page. How do I locate a panel inside a user control from javascript on the page?

    Thank you.
     

  • Re: Locate control inside User control

    05-09-2008, 4:09 PM

    Ya, this can get a bit tricky.  First off, when creating javascript on the fly, "ClientID" is your friend.  Every control has this property.  It gives you the actual value of the id when rendered.

    So what I would do is this.  Setup your javascript in your aspx page with this:

    document.getElementById('<%= GetUserControlClientID() %>')...

     GetUserControlClientID() is the name of a method in your code behind.  Feel free to rename the method.  This method should return a string.

    Inside the method call the UserControl's FindControl method to locate the control you're looking for.  Then return the value of this control's ClientID property.

    It should render correctly with the correct id in your javascript. 

  • Re: Locate control inside User control

    05-09-2008, 4:57 PM
    • Loading...
    • mthakershi
    • Joined on 05-07-2007, 5:22 PM
    • Posts 53

    That worked. But I have one more confusion.

    M = Master page

    D = Base page (from M)

    C = User control on D

    Question is how do I distribute JavaScript code between D and C?

    To give you an example:

    I have a slider in user control C. But it requires some javascript to be added in 'function pageLoad(sender, e)'. I tried pageLoad in D and C but only one will be called. So I have it in C. Now there is a javascript variable called isSliding in user control and it must be accessed where pageLoad is so how do i communicate these things? How to share it?

    Basically, my doubt is centered around how to add javascript?

    Please let me know if you can't understand. 

  • Re: Locate control inside User control

    05-09-2008, 7:10 PM

    Indifferent  I'm a bit confused by your example.  But if you need some javascript to execute when the page loads then don't wrap it in a function.  Functions must be called explicitly to run but anything else will execute as the browser renders it.

  • Re: Locate control inside User control

    05-12-2008, 10:57 AM
    • Loading...
    • mthakershi
    • Joined on 05-07-2007, 5:22 PM
    • Posts 53

    Sorry for confusion.

    Example: pageLoad in a page derived from a master page and pageLoad in a user control included in page. Can both of them execute? From my debugging I found no.

    Thank you for your help. 

Page 1 of 1 (5 items)