Emma
Emma

Reputation: 657

Can we access an ASP.NET web user control's child controls using JavaScript

I have a web user control. I want to know how to access its child control, using JavaScript, when we use it in another page.

Suppose my control is as below:

  <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCS.ascx.cs" Inherits="UCS" %>
        <asp:Panel ID="rbtnPanel" runat="server">
        <asp:RadioButton ID="rbtnEn" runat="server" GroupName="scan" Checked="true" Text="Entire" style="margin-left: -7px;"/>
        <asp:RadioButton ID="rbtnCu" runat="server" GroupName="scan" Text="Custom" style="position: absolute; margin-left: 2px;"/>
         </asp:Panel>

update:

I have added it in the page as: Try.aspx

 <%@ Register Src="~/UserControls/UCS.ascx" TagName="UCS" TagPrefix="ucSc" %>
 <table>
 <tr>
 <td>
          <usSc:UCS id="UCS1" runat="server" />
 </td>
 </tr>
 </table>

try.aspx: JavaScript:

  $(document).ready(function() {
    setPageLayout(2, 0, true);
    startTimer();
  }

  function startTimer()
  {
    alert(document.getElementById("<%= rbtnCu.ClientID %>").checked);
  }

So if I use this control in another page by registering it. How can I access the RadioButton in that Try.aspx page's JavaScript section.

Upvotes: 0

Views: 1273

Answers (1)

Adil
Adil

Reputation: 148180

You can use id selector, since the ClientIDMode for radio button is not static you will have to use ClientID as the id generated by asp.net is not what you see as a server id.

var rbrbtnEn =  document.getElementById('<%= rbtnEn.ClientID %>');

To get the checked status

alert(document.getElementById('<%= rbtnEn.ClientID %>').checked);

Upvotes: 0

Related Questions