2014-12-07 47 views
0

我已经继承了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 &nbsp|&nbsp          
       <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>&nbsp|&nbsp 
        <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>--%><%--&nbsp|&nbsp--%> 
        <asp:Literal ID="LtModifiedBy" runat="server" Text='<%#EncoderService.HtmlEncode(Curriculum.DataAccess.Membership.Users.GetUserDiplayNameForUsername(DataBinder.Eval(Container.DataItem, "DocumentModifiedBy") as string))%>'></asp:Literal>&nbsp|&nbsp 
        <%--<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事件中做到这一点,因为我没有看到访问属性值的方式。

对此有任何想法吗?

+0

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2014-12-07 23:41:13

+0

嗯,它实际上不是一个标签,而是我想如何做,但鉴于相似性,我认为这是可以接受的 – 2014-12-08 00:18:08

+0

它感觉更像元数据而不是数据,如果你知道我的意思。 – 2014-12-08 03:53:31

回答

0

那么,我最终通过一个非常简单的解决方案找到了解决这些问题的方法(老实说,我不知道为什么我以前没有想到它)。

这里的答案是捕获LtModifiedDate文字控件的OnDataBinding事件。

<asp:Literal ID="LtModifiedDate" runat="server" OnDataBinding="LtModifiedDate_DataBinding" Text='<%# Utilities.FormatDateTimeToDisplay(DataBinder.Eval(Container.DataItem, "DocumentModifiedDate")) %>'></asp:Literal> 

有了这个,我所要做的就是评估的日期,如果是DateTime.MinValue我先走一步,设置Visible属性(我加的)对DocumentModifiedInfoPanel DIV,默认定义为true。

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; 
    } 
} 

就这么简单!