在更新面板内的中继器内的用户控件内创建的 JQuery 函数不起作用



当我将用户控件移出转发器并(仍在更新面板中(手动传递其属性而不是使用中继器时,一切正常。但是当我使用转发器将属性传递给我的用户控件时,用户控件会从数据库很好地加载并出现在最终客户端页面上,该页面显示这些参数(PID、StID(成功通过,但其 jQuery 函数停止工作。所以jQuery在中继器之外也能正常工作。

我尝试过的事情:

  1. 由于他们说中继器弄乱了元素的唯一ID,所以我改用了class。这行不通。我还为类和元素 ID 使用了唯一的 ID,这也不起作用。现在我正在为中继器中的类使用唯一的名称,因为我的 PID 是一个 GUID,我在那里为类名构造了独特的东西。

  2. 已将 myusercontrol 移出转发器。这行得通。

  3. 我创建了另一个页面,并手动为我的用户控件提供属性。这工作正常。

  4. 我编写了一个与您在帖子中看到但在转发器外部看到的脚本类似的脚本,以简单地隐藏和显示我在中继器内用户控件中的元素。 但这也没有用!我什至无法在最后一页>视图源中看到用户控件的 html 输出(我猜 MS 使用不同的机制来隐藏它们(,尽管我在最后一页上看到了用户控件!除非我将用户控件放在中继器之外或使用 chrome 开发工具,否则我可以看到用户控件的 html 输出。我不确定这是否与此有关。

我的页面.aspx

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<asp:Panel ID="Panel1" runat="server" Height="49px" Style="margin-left: 211px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSrc1" ViewStateMode="Enabled" EnableViewState="False">
<ItemTemplate>
<div id='FC<%# Eval("PID")%><%# Eval("SID")%>'>
<UserControl:MView runat="Server" PelID='<%#Eval("PID")%>' StID='<%#Eval("SID") %>' ID="Mview1" ViewStateMode="Enabled" EnableViewState="False" />                            
</div>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSrc1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT PID, SID FROM [fc] LEFT JOIN [pt] ON [pt].ID = [fc].PID WHERE ([pt].[TID] = @TID)">
<SelectParameters>
<asp:ControlParameter ControlID="TreeView1" Name="TID" PropertyName="SelectedValue" DefaultValue="5AF4D771-9DA9-46B9-8075-200DF8652063" Type="String" />
</SelectParameters>
</asp:SqlDataSource>  
</ContentTemplate>    
</asp:UpdatePanel>           
</asp:Panel>

用户.Ascx

$(document).ready(function () {
$("body").on( 'click', "#front<%= PID.ToString()%><%=StID.ToString()%>",function (e) {
alert('test');
$("div.divBC<%= PID.ToString()%><%=StID.ToString()%>").hide();
$("div.divCF<%= PID.ToString()%><%=StID.ToString()%>").show();
e.preventDefault();
e.disablePopup();
return false;
});
$("body").on('click', "#back<%= PID.ToString()%><%=StID.ToString()%>", function (e) {
alert("before Back");
$("div.divCF<%= PID.ToString()%><%=StID.ToString()%>").hide();
$("div.divBC<%= PID.ToString()%><%=StID.ToString()%>").css('visibility', 'visible');
$("div.divBC<%= PID.ToString()%><%=StID.ToString()%>").show();
e.preventDefault();
e.disablePopup();
return false;
});
});

</script>
<table style="width: 100%;">
<tr>
<td>
<asp:Label ID="Lable1" runat="server" Text="--"></asp:Label></td>
<td>
<asp:Label ID="Label2" runat="server" Text="--"></asp:Label></td>
<td>
<asp:Label ID="Label3" runat="server" Text="--"></asp:Label></td>
</tr>
<tr>
<td colspan="3" >
<hr style="color: #C0C0C0" />
<span>
Time: 
<asp:Literal runat="server" ID="SpecialTime"></asp:Literal>
&nbsp;&nbsp;&nbsp;
Last Time : [under construction! ]
<asp:Literal runat="server" ID="OtherThings"></asp:Literal>
</span>
<hr />
<div class="divCF<%= PID.ToString()%><%=StID.ToString()%>">
<hr/>
<h3>
<span>                            
<br />
<asp:Literal runat="server" ID="Title"></asp:Literal>
</span>
</h3>
</div>
<div class="divBC<%= PID.ToString()%><%=StID.ToString()%>" style="visibility:hidden">                    
<asp:Literal runat="server" ID="TextCnt"></asp:Literal>
<input type="hidden" value="<%= PID.ToString() %>" id="PDHolder"/>
<input type="hidden" value="<%= StID.ToString() %>" id="StIDHolder"/>
<hr/>
</div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;           
<button id="front<%= PID.ToString()%><%=StID.ToString()%>">Front</button>
<button id="back<%= PID.ToString()%><%=StID.ToString()%>">Back</button> 
</center>
</td>
<td>&nbsp;</td>
</tr>
</table>

重复

<button id="front">

$("body").on( 'click', "#front",function (e)

不要混合均匀。

另一件事。在一个页面中只能包含一次 jQuery javascript。

最新更新