使用Jquery从MasterPage访问子页上的控件

Access a control on the Child Page from MasterPage using Jquery

本文关键字:控件 访问 Jquery MasterPage 使用      更新时间:2024-06-23

我的一个子页面中有一个按钮(rbtnDelete),我想使用JQuery在主页中获得Delete按钮的客户端ID我试了很多脚本,但没有得到任何答案

注意:我正在使用telerik radbutton

客户端页面代码:

<%@ 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>

母版页代码

...
...
 <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>

如果我使用此代码获取位于子页内的按钮的ClientIDrbtnDelete,则会出现控件不存在的错误(因为它在子页内)请帮我解决这个问题,或者给我如何从父页访问子控件的解决方案或者给我这个的解决方案

提前感谢。

您正在使用ASP.NET 4.0吗?如果是,请将rbtnDelete的ClientIDMode设置为"静态",然后您可以使用:-

$("#rbtnDelete").click();

分配给按钮的AutoID将在末尾保留原始服务器ID,因此:http://api.jquery.com/attribute-ends-with-selector/

此外,您可以使用CssClass属性为其提供一个唯一的类名,并使用jQuery通过该选择器获取它。

此外,为什么您的母版页应该关心内容页?我认为这里需要重新思考一下逻辑。

哦,第三个选项-创建一个函数,该函数将返回内容页面本身所需的内容,例如:

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

并在需要时从主页调用:

if(getMyButtonClientId) var theIdINeed = getMyButtonClientId();