C# 根据UserControl上的数据绑定值隐藏Div
我继承了ASP.NET应用程序的维护,现在不得不开发一个让我有点挠头的功能,尽管我知道答案一定很简单,因为现在我找不到这个答案 所以我在UserControl中有一个Repeater控件。对于该中继器,数据绑定在代码隐藏上,然后相应地显示在中继器的子项上。 每次一个项目绑定到后面的代码上时,都会有一个函数在执行,我并不真正理解它在做什么 这是asp.net代码:C# 根据UserControl上的数据绑定值隐藏Div,c#,asp.net,data-binding,event-handling,C#,Asp.net,Data Binding,Event Handling,我继承了ASP.NET应用程序的维护,现在不得不开发一个让我有点挠头的功能,尽管我知道答案一定很简单,因为现在我找不到这个答案 所以我在UserControl中有一个Repeater控件。对于该中继器,数据绑定在代码隐藏上,然后相应地显示在中继器的子项上。 每次一个项目绑定到后面的代码上时,都会有一个函数在执行,我并不真正理解它在做什么 这是asp.net代码: <asp:Repeater ID="rptDocumentsForCategory" runat="server">
<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>
现在,HideDocumentsDetailInfo方法是我写的。您可以试着在DocumentModifiedInfo面板内的第一行上看到我在使用它。
这种方法几乎可以工作,但是附件文档列表上的最后一项将始终显示带有空修改日期的div,而不是hding
我还注意到rptDocCategories_ItemDataBound方法在我自己的HideDocumentsDetailInfo之后执行,我怀疑这可能是我无法隐藏要隐藏的项列表中最后一项的原因
最后一点,我有点不知所措,如果以及如何在ItemDataBound事件中做到这一点,因为我没有看到访问属性值的方法
你对此有什么想法吗?嗯,我最终找到了一个解决这些问题的方法,一个非常简单的解决方案,老实说,我不知道为什么我之前没有想到 这里的答案是捕获LtModifiedDate文本控件的OnDataBinding事件
<asp:Literal ID="LtModifiedDate" runat="server" OnDataBinding="LtModifiedDate_DataBinding" Text='<%# Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentModifiedDate")) %>'></asp:Literal>
就这么简单 我已经编辑了你的标题。请看,如果共识是否定的,他们就不应该这样。好吧,事实上这不是一个标签,而是我试图做的,但是考虑到相似性,我想这是可以接受的。如果你知道我的意思,它更像元数据而不是数据。
<asp:Literal ID="LtModifiedDate" runat="server" OnDataBinding="LtModifiedDate_DataBinding" Text='<%# Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentModifiedDate")) %>'></asp:Literal>
protected void LtModifiedDate_DataBinding(object sender, EventArgs e)
{
//Cast the sender to it's type
Literal lit = (Literal)sender;
//Convert the DateTime Text value to a DateTime
DateTime dt = Convert.ToDateTime(lit.Text);
if (dt == DateTime.MinValue)
{
//Cast the parent control (the div) to a HtmlGenericControl
HtmlGenericControl div = (HtmlGenericControl)lit.Parent;
//Set the visible property to false
div.Visible = false;
}
}