我已經繼承了ASP.NET應用程序的維護,現在必須開發一個功能,該功能稍微彎曲了我的腦海,儘管我知道現在的答案必須很簡單這個答案是躲過我..根據UserControl上的數據綁定值隱藏Div
所以我有一個用戶控件內的Repeater控件。對於此中繼器,數據綁定在代碼隱藏之後,然後相應地顯示在中繼器的子項目上。 每次在代碼隱藏中綁定一個Item時,都會有一個函數執行,我並不真正瞭解它在做什麼。
這是asp.net代碼:
<asp:Repeater ID="rptDocumentsForCategory" runat="server">
<ItemTemplate>
<div style="padding: 0px 10px;">
<a target="_blank" href='<%#EncoderService.HtmlAttributeEncode(DataBinder.Eval(Container.DataItem, "DocumentUrl") as string)%>'><asp:Literal ID="Literal1" runat="server" Text='<%#EncoderService.HtmlEncode(DataBinder.Eval(Container.DataItem, "DocumentName") as string)%>'></asp:Literal></a>
<%if (Mode == Curriculum.BLL.ControlMode.Edit)
{ %>
<asp:ImageButton ID="btnEdit" Height="10px" ImageUrl="~/Styles/Images/edit.png" ToolTip='<%$Resources:Main,Dialog_Title_UpdateWorkDocument%>' CommandName="EditItem" CommandArgument='<%#Eval("DocumentId")%>' runat="server" />
<asp:ImageButton ID="btnDelete" Height="10px" OnClientClick="javascript:return ShowConfirmationPopup();" ImageUrl="~/Styles/Images/delete.png" ToolTip='<%$Resources:Main,DeleteDocument%>' CommandName="DeleteItem" CommandArgument='<%#Eval("DocumentId")%>' runat="server" />
<%} %>
<div class="ViewWorkDocumentsFileData">
<%--<asp:Literal ID="LtDocDate" runat="server" Text='<%#Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentDate"))%>'></asp:Literal>--%>
<asp:Literal ID="LtDocSize" runat="server" Text='<%#Utilities.ConvertBytesToKilobytes((int)DataBinder.Eval(Container.DataItem, "DocumentSize"))%>'></asp:Literal> Kb  | 
<asp:Literal ID="LtDocExt" runat="server" Text='<%#EncoderService.HtmlEncode(DataBinder.Eval(Container.DataItem, "DocumentExtension") as string)%>'></asp:Literal>
<div ID="DocumentCreationInfoPanel" runat="server" >
<asp:Literal ID="LtCreatedByLabel" runat="server" Text='<%$Resources:Main,CreatedBy_Label%>'></asp:Literal>
<asp:Literal ID="LtDocModification" runat="server" Text='<%#EncoderService.HtmlEncode(Curriculum.DataAccess.Membership.Users.GetUserDiplayNameForUsername(DataBinder.Eval(Container.DataItem, "DocumentCreatedBy") as string))%>'></asp:Literal> | 
<asp:Literal ID="LtDocDate" runat="server" Text='<%#Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentDate"))%>'></asp:Literal>
</div>
<div ID="DocumentModifiedInfoPanel" runat="server" >
<asp:Literal ID="LtModifiedByLabel" runat="server" Text='<%$Resources:Main,ModifiedBy_Label%>'></asp:Literal>
<%--<asp:Literal ID="LtModifiedBy" runat="server" Text='<%#EncoderService.HtmlEncode((DataBinder.Eval(Container.DataItem, "DocumentModifiedBy") != DBNull.Value) ? Curriculum.DataAccess.Membership.Users.GetUserDiplayNameForUsername(DataBinder.Eval(Container.DataItem, "DocumentModifiedBy") as string) : HideDocumentsDetailInfo(DataBinder.Eval(Container.DataItem, "DocumentModifiedBy")))%>'></asp:Literal>--%><%-- | --%>
<asp:Literal ID="LtModifiedBy" runat="server" Text='<%#EncoderService.HtmlEncode(Curriculum.DataAccess.Membership.Users.GetUserDiplayNameForUsername(DataBinder.Eval(Container.DataItem, "DocumentModifiedBy") as string))%>'></asp:Literal> | 
<%--<asp:Literal ID="LtModifiedDateLabel" runat="server" Text='<%$Resources:Main,ModifiedDate_Label%>'></asp:Literal>--%>
<%--<asp:Literal ID="LtModifiedDate" runat="server" Text='<%#Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentModifiedDate") as string) %>'></asp:Literal>--%>
<asp:Literal ID="LtModifiedDate" runat="server" Text='<%#Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentModifiedDate")) %>'></asp:Literal>
</div>
</div>
</div>
</ItemTemplate>
<SeparatorTemplate>
<div></div>
</SeparatorTemplate>
</asp:Repeater>
現在,大部分的代碼已經在那裏了。我在這裏添加的是DocumentModifiedInfoPanel中的代碼,用於向頁面的該部分顯示修改日期和用戶(它與「附件」文檔有關)。 現在,如果我沒有修改日期,該div應該被隱藏(它仍然可以渲染,但仍然不可見),我無法做到這一點。
這裏是我的(相關)後臺代碼:
protected void rptDocCategories_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.EditItem ||
e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.SelectedItem)
{
Repeater rptDocumentsForCategory = (Repeater)e.Item.FindControl("rptDocumentsForCategory");
rptDocumentsForCategory.ItemCommand += new RepeaterCommandEventHandler(rptDocumentsForCategory_ItemCommand);
}
}
protected void rptDocCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.EditItem ||
e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.SelectedItem)
{
Literal LtDocCategoryId = (Literal)e.Item.FindControl("LtDocCategoryId");
Repeater rptDocumentsForCategory = (Repeater)e.Item.FindControl("rptDocumentsForCategory");
var ds = WorksDocumentCategories.GetDocumentsForCategoryAndWork(WorkId, int.Parse(LtDocCategoryId.Text));
rptDocumentsForCategory.DataSource = ds;
rptDocumentsForCategory.DataBind();
}
}
public string HideDocumentsDetailInfo(object DBValue)
{
foreach (RepeaterItem item in rptDocCategories.Items)
{
HtmlGenericControl div = (HtmlGenericControl)Utilities.Utilities.FindControlRecursive(item, "DocumentModifiedInfoPanel");
if (div.ID == "DocumentModifiedInfoPanel" && DBValue == DBNull.Value)
{
div.Attributes.Add("style", "display: none");
}
}
return "";
}
現在,HideDocumentsDetailInfo
方法是我寫的。您可以嘗試在DocumentModifiedInfoPanel
的第一條註釋行中看到我使用它。 這種方法幾乎可行,但附件文檔列表中的最後一項將始終顯示帶有「空白」修改日期而不是hdding的div。
我也注意到rptDocCategories_ItemDataBound
方法在我自己的HideDocumentsDetailInfo
後執行,我懷疑這可能是我無法隱藏要隱藏項目「列表」中最後一項的原因。
作爲一個最後的筆記,我有點失落,如果我能在ItemDataBound事件中做到這一點,因爲我沒有看到訪問屬性值的方式。
對此有任何想法嗎?
我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-12-07 23:41:13
嗯,它實際上不是一個標籤,而是我想如何做,但鑑於相似性,我認爲這是可以接受的 – 2014-12-08 00:18:08
它感覺更像元數據而不是數據,如果你知道我的意思。 – 2014-12-08 03:53:31