ASP.网络中继器响应控件绑定



我有ASP。Net控件用于搜索属性,并使用复选框选项和asp.net。网络中继器。对于响应式设计,我在两个不同的地方有相同的绑定来匹配设计。

此处,CheckBoxList ID=Options1 &ID=Options2都具有相同的绑定,但代码将重复。我有400行来检查单个绑定的条件。我必须为另一个视图再次复制所有代码。是否有任何方法来优化代码与两个视图处理的单一绑定。(避免重复绑定和检查)?

// Desktop View
<div class="hidden-sm hidden-xs narrowSearch">
//Content
<asp:Repeater ID="rptAttributes1" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options1" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
// Mobile view
 <div class="sec_left  hidden-lg hidden-md"> 
 <div class="moremenu narrowSearch">
//Content
 <asp:Repeater ID="rptAttribute2" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options2" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
</div>

背后的代码:

 CheckBoxList chklOptions1 = item.FindControl("Options1") as CheckBoxList;
 CheckBoxList chklOptions2 = item.FindControl("Options2") as CheckBoxList;

您可以将您的中继器(包括声明性代码以及您在页面代码后面的代码)放置到自定义用户控件中。然后将用户控件放在页面的每个部分(桌面和移动)中。这是避免重复代码的最简单方法。话虽如此,由于您只提供了页面的代码隐藏类中的部分代码,因此当您将页面的代码隐藏类中的命令式代码移动到用户控件中时,我无法确定需要进行哪些更改。

祝你好运!

最新更新