javascript - 转发器定义将从设计器中的嵌套转发器中删除

标签 javascript html asp.net

我有一个嵌套的转发器,每次编译项目时,VS都会自动从设计器中删除内部转发器的声明,我必须再次手动添加它。

protected global::System.Web.UI.WebControls.Repeater rptrSubscriptions;

我已经删除了 VS 临时文件,在阅读了像我这样的其他问题后,我还将页面转换为网络表单。 最后我将内部中继器更改为GridView,因为我认为嵌套中继器可能存在错误,但我仍然遇到同样的问题。

为了测试嵌套转发器是否正常工作,我添加了一个名为repeater1的空转发器,并且它工作正常:它不会在编译时被删除。 如有任何建议,我们将不胜感激。

<%--================================================================--%>
<asp:Repeater ID="rptrSubscriptionGroups" runat="server" OnItemDataBound="rptrSubscriptionGroups_ItemDataBound" OnItemCommand="rptrSubscriptionGroups_ItemCommand">
    <ItemTemplate>
        <div>
            <asp:Table ID="Table1" runat="server" BorderStyle="Solid" BorderWidth="1" Width="950">
                <asp:TableRow CssClass="SolidBorder">
                    <%--SHow Subscriptions ( Left Side )--%>
                    <asp:TableCell Width="550">
//some source here
                    </asp:TableCell>
                    <asp:TableCell>
                        <div class="">
                            <%--SHow Small TV+Radio Images--%>
                            <a href="#" style="text-decoration: none">
                                <asp:ImageButton ID="imgTVRadio" alt="" Style="width: 220px; height: 50px;" runat="server" OnCommand="SubscriptionGroup_Click" CommandName="SubscriptionClick" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID")+","+DataBinder.Eval(Container.DataItem,"GroupName") %>' BorderStyle="None" />
                            </a>

                            <%-- ===========Test repeater that will not be removed from designer ====================================--%>
                            <asp:Repeater ID="Repeater1" runat="server"></asp:Repeater>

                            <%-- =========== repeater that will  be removed from designer ====================================--%>
                            <asp:Repeater ID="rptrSubscriptions" runat="server" OnItemDataBound="rptrSubscriptions_ItemDataBound" OnItemCommand="rptrSubscriptions_ItemCommand">
                                <ItemTemplate>
                                    <asp:Table ID="Table2" class="NoBorderInTable" runat="server" BorderStyle="Solid" BorderWidth="1" Width="380px">
                                        <asp:TableRow CssClass="SolidBorder" Style="background-color: lightgray; border-color: white" Height="30px">

                                            <%--Show Radio Buttons--%>
                                            <asp:TableCell>
                                        <%--<asp:RadioButton ID="RadioButton1" runat="server" />--%>
                                        <input name="SubscriptionSelected" id="SubscriptionSelected" type="radio" value="1">
                                        <%--Focuses on the selected radio button--%>
                                        <script>
                                            $('#tableSelect tr').click(function () {
                                                $(this).find('th input:radio').prop('checked', true);
                                            })
                                        </script>
                                            </asp:TableCell>

                                        </asp:TableRow>
                                    </asp:Table>
                                </ItemTemplate>
                            </asp:Repeater>
                            <%--=====================================================================================================--%>
                        </div>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </div>
    </ItemTemplate>
</asp:Repeater>

最佳答案

内部中继器或任何其他服务器控件不应位于您的设计器中。因此,如果您更改标记中的任何内容,从而触发 Designer.cs 的更新,它将删除所有内部服务器控件。

大多数情况下,中继器会包含多个项目(这就是您使用中继器的原因)。因此,如果您有 10 个项目绑定(bind)到您的中继器,您将不会在设计器中获得 10 个内部中继器。您的设计器中也不会获得 1 个内部中继器,因为它不能用于控制 10 个。

您应该在 DataItemBound rptrSubscriptionGroups_ItemDataBound 中访问内部转发器。我总是使用与标记中使用的名称相同的名称,因此您的意思很清楚:

protected void rptrSubscriptionGroups_ItemDataBound(object sender, RepeaterItemEventArgs args) {
    //Get the inner repeater in the current repeater-item
    Repeater rptrSubscriptions = args.Item.FindControl("rptrSubscriptions") as Repeater;
    if (rptrSubscriptions != null) {
        //Do what you need todo for this inner repeater
        rptrSubscriptions.DataSource = someSource; //args.Item.DataItem should be the DataSource-item you bound to rptrSubscriptionGroups so use that to sort your Source for the inner repeater
        rptrSubscriptions.DataBind();
    }
}

关于javascript - 转发器定义将从设计器中的嵌套转发器中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30918983/

相关文章:

javascript - window.print() 方法停止世界

javascript - 使用网络音频 api 分析来自麦克风的输入(将 MediaStreamSource 转换为 BufferSource)

javascript - 使用字符串数组中找到的键添加/删除数组对象

javascript - 为什么我的 JavaScript 代码会过早触发?

c# - 必填字段验证器文本定位

javascript - 获取 React 组件中子元素的高度

java - Selenium 网络驱动程序 : IE can't find radio buttons nor checkboxes for using click() on them

javascript - 使用 Knockout 和 JayData 进行下拉刷新

c# - 在运行时更改资源的提取位置?

asp.net - 在简单明了的 HTML5 选择元素中发出警告