Access a child control contained in a User Control from javascript?

Last post 01-09-2009 8:00 AM by James Evans. 9 replies.

Sort Posts:

  • Access a child control contained in a User Control from javascript?

    01-05-2009, 11:36 AM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 2:53 AM
    • Florida
    • Posts 118

    Hi All -

    I have a UserControl that contains 3 datadropdowns. I have added the control to a content page (that has a master page) inside a tabel. I need to be able to access the selected values of the dropdown controls that are contained within the UserControl from javascript. This is a time entry UserControl and when I placed it on the page I gave it the ID of "ucPreTime" (eventually there will be a PostTime). The control contains ddlHour, ddlMinute and ddlAmPm. In view source I see that the ID that gets generated for ddlHour is "ucPreTime_ddlHour" so I have attempted many differnt version of using that ID (from googling it to death) but cannot get a handle on the control. Some of the things I have tried are:

    • var preTime = getElementById("ucPreTime_ddlHour");

    • var preTime = $get("<%=ucPreTime_ddlHour %>");

    • document.todaysdraw.elements[ucPreTime:ddlHour] - where todaysdraw is the name of the aspx page.

    Any help with this ?

    Thanks.

     

  • Re: Access a child control contained in a User Control from javascript?

    01-05-2009, 12:13 PM
    • All-Star
      76,257 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,209
    • TrustedFriends-MVPs

    Here's an example:

    The TextBox TextBox1 is inside of the User Control.

    <form id="form1" runat="server">
     <uc1:WebUserControl ID="WebUserControl1" runat="server" />
    </form>

    <script type="text/javascript">
    <!--
    function getUserControlTextBoxId()
    {
     var textBoxId = '<%= ((TextBox)WebUserControl1.FindControl("TextBox1")).ClientID %>';

     return var textBoxId;
    }
    // -->
    </script>

    NC...

  • Re: Access a child control contained in a User Control from javascript?

    01-05-2009, 1:15 PM
    • Member
      216 point Member
    • shyam14111986
    • Member since 08-26-2008, 12:48 PM
    • Banglore
    • Posts 40

     

    James Evans:

    Hi All -

    I have a UserControl that contains 3 datadropdowns. I have added the control to a content page (that has a master page) inside a tabel. I need to be able to access the selected values of the dropdown controls that are contained within the UserControl from javascript. This is a time entry UserControl and when I placed it on the page I gave it the ID of "ucPreTime" (eventually there will be a PostTime). The control contains ddlHour, ddlMinute and ddlAmPm. In view source I see that the ID that gets generated for ddlHour is "ucPreTime_ddlHour" so I have attempted many differnt version of using that ID (from googling it to death) but cannot get a handle on the control. Some of the things I have tried are:

    • var preTime = getElementById("ucPreTime_ddlHour");

    • var preTime = $get("<%=ucPreTime_ddlHour %>");

    • document.todaysdraw.elements[ucPreTime:ddlHour] - where todaysdraw is the name of the aspx page.

    Any help with this ?

    Thanks.

     

     Hi James,

    Happy New Year. For accessing child control in client side you can use getElementsByTagName() method. In your case you need to access drop down lists which get rendered as <input type="select"> in the client's browser. This is what you need to do.

    Step 1 : Registering ClientID in a client side array

    When you use master pages, the IDs of the controls which are finally rendered in the browser are prefixed with "ct100_<content place holder ID>" . So to access these elements from javascript, you can register their clientIDs in client side arrays. To do this, the following code is used.

     

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript("CONTROL","'"+ctrl.ClientID+"'");
        //Rest of your page load logic.
    }

     

    On page_load the clientID of your control (ctrl is used as an example here) is registered to a client side array. Now in javascript CONTROL[0] stores the client ID of the control. (CONTROL[0] = 'ct100_cphContentPage_ctrl'). The advantage with this method is you can hive off the javascript functions to a seperate file.

    Step 2 : Javascript function to obtain the selected value of all the drop down list child controls of the user control

     Here, getElementsByTagName method can be used. Since all the drop down lists are rendered as <input type="select"> "INPUT" is passed as a parameter to this method. To do this, the following code is used.

     

    function fnGetSelectedValues()
    {
        var UserControl = document.getElementById(CONTROL[0]);
        var arrInput = UserControl.getElementsByTagName("INPUT");
        for(var i=0;iif(arrInput[i].type == "select")
            {
                //Obtain the selected value and perform necessary operations
            }
        }
    }

      This gives you full access to all the drop down lists in you User Control. You can add this function to the OnClientClick event of the Submit button to perform validations on the client side.

    If you face any issues with this design please let me know.

    Please mark the post as answer if it helped you.

    Shyam Sundar Vasudevan
    Developer
  • Re: Access a child control contained in a User Control from javascript?

    01-05-2009, 2:16 PM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 2:53 AM
    • Florida
    • Posts 118

    My bad -

    I forgot that I'm doing this page as a pop-up and there is no master page - sory.

    Anyway - in View Source - the user control is not being rendered as a container at all - each drop down is being rendered seperatly a "select" tag with a list of "options". The name of the user control is being appended to the front as in the original post illustrates. "ucPreTime_ddlHour - here is an excerpt from the View Source for the ddlHour that is contained in the ucPreTime User Control:

     <select name="ucPreTime$ddlHour" id="ucPreTime_ddlHour" style="font-family:Areal;font-size:8pt;">

     <option value="01">01</option>
     <option value="02">02</option>
     <option value="03">03</option>
     <option value="04">04</option>
     <option value="05">05</option>
     <option value="06">06</option>
     <option value="07">07</option>
     <option value="08">08</option>
     <option value="09">09</option>
     <option value="10">10</option>
     <option value="11">11</option>
     <option value="12">12</option>

    </select>

    Thanks Again

  • Re: Access a child control contained in a User Control from javascript?

    01-06-2009, 7:25 AM
    Answer
    • All-Star
      76,257 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,209
    • TrustedFriends-MVPs

    Maybe I wasn't clear enough in my post?

    Default.aspx

    <%@ Page Language="C#" Debug="true" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Default Page</title>
    </head>
    <body>
     <form id="form1" runat="server">
      <input type="button" onclick="onButtonClick();" value="Display UC DDL ID" />
      <uc1:WebUserControl ID="WebUserControl1" runat="server" />
     </form>
    </body>
    </html>

    <script type="text/javascript">
    <!--
    function getUserControlDropDownListId()
    {
     var dropDownListId = '<%= (WebUserControl1.FindControl("DropDownList1")).ClientID %>';

     return dropDownListId;
    }
    // -->
    </script>

    <script type="text/javascript">
    <!--
    function onButtonClick(elementRef)
    {
     alert(getUserControlDropDownListId());
    }
    // -->
    </script>

    WebUserControl.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

    <asp:DropDownList id="DropDownList1" runat="server">
     <asp:listitem value="">Select one</asp:listitem>
     <asp:listitem value="1">Item 1</asp:listitem>
     <asp:listitem value="2">Item 2</asp:listitem>
     <asp:listitem value="3">Item 3</asp:listitem>
     <asp:listitem value="4">Item 4</asp:listitem>
     <asp:listitem value="5">Item 5</asp:listitem>
     <asp:listitem value="6">Item 6</asp:listitem>
    </asp:DropDownList>

    NC...

  • Re: Access a child control contained in a User Control from javascript?

    01-06-2009, 9:59 AM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 2:53 AM
    • Florida
    • Posts 118

    Sory - forgot to mention. I tried this and the app would not even compile. 

    NC01:

    Maybe I wasn't clear enough in my post?

    Default.aspx

    <%@ Page Language="C#" Debug="true" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Default Page</title>
    </head>
    <body>
     <form id="form1" runat="server">
      <input type="button" onclick="onButtonClick();" value="Display UC DDL ID" />
      <uc1:WebUserControl ID="WebUserControl1" runat="server" />
     </form>
    </body>
    </html>

    <script type="text/javascript">
    <!--
    function getUserControlDropDownListId()
    {
     var dropDownListId = '<%= (WebUserControl1.FindControl("DropDownList1")).ClientID %>';

     return dropDownListId;
    }
    // -->
    </script>

    <script type="text/javascript">
    <!--
    function onButtonClick(elementRef)
    {
     alert(getUserControlDropDownListId());
    }
    // -->
    </script>

    WebUserControl.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

    <asp:DropDownList id="DropDownList1" runat="server">
     <asp:listitem value="">Select one</asp:listitem>
     <asp:listitem value="1">Item 1</asp:listitem>
     <asp:listitem value="2">Item 2</asp:listitem>
     <asp:listitem value="3">Item 3</asp:listitem>
     <asp:listitem value="4">Item 4</asp:listitem>
     <asp:listitem value="5">Item 5</asp:listitem>
     <asp:listitem value="6">Item 6</asp:listitem>
    </asp:DropDownList>

    NC...

  • Re: Access a child control contained in a User Control from javascript?

    01-06-2009, 11:26 AM
    • All-Star
      76,257 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,209
    • TrustedFriends-MVPs

    Weird, cause it sure worked for me. What are you doing different than I am? I cut that code straight from a working example.

    NC...

     

  • Re: Access a child control contained in a User Control from javascript?

    01-08-2009, 3:04 PM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 2:53 AM
    • Florida
    • Posts 118

    NC01 - 

    Finally got this to wrok - thanks - very nice and saved me! 

    NC01:

    Maybe I wasn't clear enough in my post?

    Default.aspx

    <%@ Page Language="C#" Debug="true" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Default Page</title>
    </head>
    <body>
     <form id="form1" runat="server">
      <input type="button" onclick="onButtonClick();" value="Display UC DDL ID" />
      <uc1:WebUserControl ID="WebUserControl1" runat="server" />
     </form>
    </body>
    </html>

    <script type="text/javascript">
    <!--
    function getUserControlDropDownListId()
    {
     var dropDownListId = '<%= (WebUserControl1.FindControl("DropDownList1")).ClientID %>';

     return dropDownListId;
    }
    // -->
    </script>

    <script type="text/javascript">
    <!--
    function onButtonClick(elementRef)
    {
     alert(getUserControlDropDownListId());
    }
    // -->
    </script>

    WebUserControl.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

    <asp:DropDownList id="DropDownList1" runat="server">
     <asp:listitem value="">Select one</asp:listitem>
     <asp:listitem value="1">Item 1</asp:listitem>
     <asp:listitem value="2">Item 2</asp:listitem>
     <asp:listitem value="3">Item 3</asp:listitem>
     <asp:listitem value="4">Item 4</asp:listitem>
     <asp:listitem value="5">Item 5</asp:listitem>
     <asp:listitem value="6">Item 6</asp:listitem>
    </asp:DropDownList>

    NC...

  • Re: Access a child control contained in a User Control from javascript?

    01-09-2009, 7:43 AM
    • All-Star
      76,257 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,209
    • TrustedFriends-MVPs

    I thought that you said that the code wouldn't even compile. So what did you have to change to make it compile?

    NC...

  • Re: Access a child control contained in a User Control from javascript?

    01-09-2009, 8:00 AM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 2:53 AM
    • Florida
    • Posts 118

    It was something that I typod in the javascript. I ended up having to cut an paste the following line from you and then replacing my values for the user control and dropdown names:

    var dropDownListId = '<%= (WebUserControl1.FindControl("DropDownList1")).ClientID %>'; 

    NC01:

    I thought that you said that the code wouldn't even compile. So what did you have to change to make it compile?

    NC...

Page 1 of 1 (10 items)