我不能在<asp:multiview>中访问html元素

I can't access an html element inside <asp:multiview>

本文关键字:元素 访问 multiview html asp 不能      更新时间:2023-09-26

如何使用jquery或javascript访问<asp:multiview>内部的html元素?

假设我有multiview的结构:

<div runat="server" class="tabContents" style="height:100%; width:100%;">
    <asp:MultiView id="MultiView1" ActiveViewIndex="0" Runat="server">
        <asp:View ID="v1" runat="server" >
            <iframe id="f1" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>        
        <asp:View ID="v2" runat="server" >
            <iframe id="f2" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v3" runat="server" >
            <iframe id="f3" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v4" runat="server" >
            <iframe id="f4" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v5" runat="server" >
            <iframe id="f5" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v6" runat="server" >
            <iframe id="f6" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>        
        <asp:View ID="v7" runat="server" >
            <iframe id="f7" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v8" runat="server" >
            <iframe id="f8" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v9" runat="server" >
            <iframe id="f9" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
        <asp:View ID="v10" runat="server" >
            <iframe id="f10" runat="server" style="border: None; height: 100%; width: 100%;"></iframe>
        </asp:View>
    </asp:MultiView>
</div>

我试过下面的代码:

 string s;
        s =
            "<script>" +
            "$('#f" + index++ + "').src(" + "'" + url + "'" + ");" + 
            "</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ExeCuteScript", s);

但是不能用

请帮我一下。谢谢你!

我以前从未使用过multiviews,但如果f1可以从您的代码后面访问,那么您的jquery可以像以下一样简单:

$('#<%= f1.ClientID %>')

如果您查看页面的呈现标记,您将看到您试图访问的元素的id与您分配的元素的id不相同。这是因为ASP。. NET为runat="server"元素生成唯一的id。

但是,您会注意到ID的最后是您在XAML中指定的实际ID。你可以使用jQuery的"endswith"选择器,如下所示:

String.Format("$('[id$=f{0}]').src('{1}');", index++, url);

所以你的代码块变成了:

string s;
s = "<script>" +
    String.Format("$('[id$=f{0}]').src('{1}');", index++, url) + 
    "</script>";
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ExeCuteScript", s);

或者,如果你没有在你的代码后面使用你的iframe,只是删除runat="server"从他们。