Access a control on the Child Page from MasterPage using Jquery

3.3k views Asked by At

I have a button (rbtnDelete) in one of my child page , and i want to get the client-ID of button Delete in my master page using JQuery i tried many scripts but i didn't get any answer

NOTE: I am using telerik radbutton

Client Page Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterLogin/Logistic.Master" AutoEventWireup="true"
CodeBehind="CreateRunsheet.aspx.cs" Inherits="DomesticLogisticsManagement.Runsheet.CreateRunsheet" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
...
...

<telerik:RadButton ID="rbtnDelete" runat="server" Visible="false" Text="Delete" Skin="Windows7" OnClick="rbtnDelete_Click" OnClientClicking="CustomRadWindowConfirm" >
</telerik:RadButton>
...
...
</asp:Content>

Master Page Code

...
...
 <script type="text/javascript">
  function CustomRadWindowConfirm(sender, args) {
           //Open the window
           $find("<%=confirmWindow.ClientID %>").show();
           //Focus the Yes button
           $find("<%=btnYes.ClientID %>").focus();
           //Cancel the postback
           args.set_cancel(true);
      }
      function YesOrNoClicked(sender, args) {
           var oWnd = $find("<%=confirmWindow.ClientID %>");
           oWnd.close();
           if (sender.get_text() == "Yes") {
                $find("<%=rbtnDelete.ClientID %>").click();
           }
      }

 </script>
<div>
      <asp:Label ID="Label3" runat="server" EnableViewState="false" ForeColor="Green"></asp:Label>
      <telerik:RadWindow ID="confirmWindow" runat="server" VisibleTitlebar="false" VisibleStatusbar="false"
           Modal="true" Behaviors="None" Height="150px" Width="300px">
           <ContentTemplate>
                <div style="margin-top: 30px; float: left;">
                     <div style="width: 60px; padding-left: 15px; float: left;">
                          <img src="img/ModalDialogAlert.gif" alt="Confirm Page" />
                     </div>
                     <div style="width: 200px; float: left;">
                          <asp:Label ID="lblConfirm" Font-Size="14px" Text="Are you sure you want to Delete ?"
                               runat="server"></asp:Label>
                          <br />
                          <br />
                          <telerik:RadButton ID="btnYes" runat="server" Text="Yes" AutoPostBack="false" OnClientClicked="YesOrNoClicked">
                               <Icon PrimaryIconCssClass="rbOk"></Icon>
                          </telerik:RadButton>
                          <telerik:RadButton ID="btnNo" runat="server" Text="No" AutoPostBack="false" OnClientClicked="YesOrNoClicked">
                               <Icon PrimaryIconCssClass="rbCancel"></Icon>
                          </telerik:RadButton>
                     </div>
                </div>
           </ContentTemplate>
      </telerik:RadWindow>
 </div>
....
....
 </form>
 </body>
 </html>

If i use this code to get ClientID of my button rbtnDelete which is inside the Child Page ,i am getting error that control is not there (because it is inside the child page) Please help me to solve this ,either give me solution for How to Access Child Controls From Parent Page OR Give me Solution for this

Thanks in Advance.

2

There are 2 answers

2
sh1rts On

Are you using ASP.NET 4.0 ? If so, set the ClientIDMode of rbtnDelete to "Static", then you can just use: -

$("#rbtnDelete").click();
0
rdmptn On

The AutoID assigned to the button will keep the original server ID at the end, so: http://api.jquery.com/attribute-ends-with-selector/

Also, youu can use the CssClass property to give it a unique class name and get it through that selector with jQuery.

Also, why should your master page care about a content page? I think some rethinking of the logic is needed here.

Oh, a third option - create a function that will return what you need in the content page itself, e.g.:

function getMyButtonClientId() { /* get what you need */ }

and call if from the master page when needed:

if(getMyButtonClientId) var theIdINeed = getMyButtonClientId();