Accessing Control from Client Side

Last post 06-12-2007 6:27 AM by mbanavige. 10 replies.

Sort Posts:

  • Accessing Control from Client Side

    05-13-2007, 6:08 AM

    hi,

    I am developing a custom control which has child control suppose textbox.how can I get the ID of the TextBox from JavaScript?

    I have seen when the control is rendered in html format it includes master page id,content placeholder id,Clientid and then it's own id.

    How can I know whether master page exists or not?is there any system to access the control direcly by it's assigned ID?

  • Re: Accessing Control from Client Side

    05-13-2007, 7:37 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,753
    • Moderator
      TrustedFriends-MVPs

    all controls have a property called ClientId.  this property gives you the id that will be used when the control is rendered to the client browser.

    if you need this id in a script, you need to dynamically inject it into the script.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Accessing Control from Client Side

    05-13-2007, 7:54 AM

    hi,

    thanks for ur reply.Can U say in details how can I access my child control of a Composite control from script.just tell how can I get the following ID?

    TextBoxId=ctl00_+contenplaceholderID+clientId+controlID.

    document.getElementbyId(TextBoxId);

    look at the above.....here how can I know that master page exists or not?

    Filed under:
  • Re: Accessing Control from Client Side

    05-13-2007, 8:57 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,753
    • Moderator
      TrustedFriends-MVPs

    you should not try to generate your own clientid strings.

    if the javascript is in your pages markup, then you can inject your controls clientid like this:

    document.getElementbyId('<%=TextBox1.ClientId%>');

    now when the page renders, the code block <%= %> will be automatically evaluated resulting in something like this:

    document.getElementbyId('ctl00_the_complete_generated_client_id');
    
     
    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Accessing Control from Client Side

    05-13-2007, 10:47 PM

    Pls Note some Scenerion.....

    1.I am writing the script in my Custom Composite Control by using

       string   scrpt =@"function ......()

    {

            var cntl=  document.getElementbyId(...);

    };

        this.Controls.Add(new Literal Control(scrpt));

    2. The Control I want to access in the avobe is a child control of the Composite Control. (suppose , it is a TextBox).

    now tell mecan I use in this case........

    document.getElementbyId('<%=TextBox1.ClientId%>'); ?????
    Actually I used this but it shows error.
  • Re: Accessing Control from Client Side

    05-14-2007, 11:44 PM
    Answer

    The situation I noticed in my control is that ...........in 'CreateChildControl' Method I have not get the full control ID with it's master pageid, contentplaeholder id and etc.....

     

    but in the method 'OnpreRender' I have found the actual rendered id of the control by simply writing control.ClientID.

     

     

    Filed under: ,
  • Re: Accessing Control from Client Side

    06-11-2007, 6:00 PM
    • Loading...
    • ckhsu
    • Joined on 06-11-2007, 9:57 PM
    • Posts 3

    I did the same thing as people mentioned:

    document.getElementbyId('<%=TextBox1.ClientId%>');

    However, when I compiled it I got a error message: The name 'TextBox1' does not exist in the current context.

    Is this because the javascript code is placed on the master page and the control is on another page?
     

  • Re: Accessing Control from Client Side

    06-11-2007, 8:17 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,753
    • Moderator
      TrustedFriends-MVPs

    ckhsu:

    Is this because the javascript code is placed on the master page and the control is on another page?

    yes

    If you are generating your javascript on your master page and TextBox1 represents a control on your content page, then you cannot directly reference TextBox1.

    From a design standpoint, its not a good idea for your master page to know much about the content page(s) it is associated with.  If your master page only works correctly with a content page that contains a TextBox specifically named TextBox1, then you really havent created a clear seperation between your master and content pages.  Instead you have coupled them tightly together and unnecessary coupling is something to be avoided.

    If the textbox is a control on the content page, then the content page should be responsible for injecting any script for that textbox.

    with that said, the way to find a control on the content page from the master page would go something like this:

        document.getElementbyId('<%=Me.FindControl("ContentPlaceHolder1").FindControl("TextBox1").ClientId%>');

    Me represents the master page
    Then we find the contentplaceholder with represents the content page
    Then we can find the textbox that on the content page that was placed inside the master page.

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Accessing Control from Client Side

    06-11-2007, 9:15 PM
    • Loading...
    • ckhsu
    • Joined on 06-11-2007, 9:57 PM
    • Posts 3

    mbanavige:

    yes

    From a design standpoint, its not a good idea for your master page to know much about the content page(s) it is associated with.  If your master page only works correctly with a content page that contains a TextBox specifically named TextBox1, then you really havent created a clear seperation between your master and content pages.  Instead you have coupled them tightly together and unnecessary coupling is something to be avoided.

    If the textbox is a control on the content page, then the content page should be responsible for injecting any script for that textbox.

     

     Thanks a lot!  Very detail explanation.  Now a realize what cause the problem and how to fix it. I think the best way should be putting my javascript on the content page itself rather than on the master page. Thanks again. Wink
     

  • Re: Accessing Control from Client Side

    06-11-2007, 9:28 PM
    • Loading...
    • ckhsu
    • Joined on 06-11-2007, 9:57 PM
    • Posts 3

    However, when I put my javascript on the content page just below the <asp:Content ID="xxx" ...>  like this:

    <script type="text/javascript">

    <!--
            function loginencrypt()
            {
                var str_in=document.getElementById('<%=txtPassword.ClientId%>').value;
                document.getElementById('<%=txtPassword.ClientId%>').value=encrypt(str_in);
            }
            //-->
        </script>

    and I still got a error message: Compiler Error Message: CS0117: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'ClientId' .

     Did I do anything wrong?

  • Re: Accessing Control from Client Side

    06-12-2007, 6:27 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,753
    • Moderator
      TrustedFriends-MVPs

    C# is case sensitive.

    Change ClientId to ClientID

     

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (11 items)
Microsoft Communities
Page view counter