سؤال

أريد تنفيذ APPERANCE هذه المقالة المذكورة باستخدام Contensview Control. ومع ذلك ، في السيناريو الخاص بي ، لا يمكنني استخدام ContityDataSource Control ، لذا أقوم بربط البيانات يدويًا.

طاولتي:

Categories
  PK: UniqueId, Guid
  Name, string
  ParentId, Guid

<asp:ListView ID="CategoryList" runat="server" 
        onitemdatabound="CategoryList_ItemDataBound">
        <LayoutTemplate>
            <table>
                <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>

        <ItemTemplate>
            <tr>
                <td colspan="2"><%# Eval("Name") %></td>
            </tr>
        </ItemTemplate>
    </asp:ListView>



protected void Page_Load(object sender, EventArgs e)
{
    using (PractiseEntities context = new PractiseEntities()) {
        var result = from categories in context.Categories
                     select categories;
        CategoryList.DataSource = result;
        CategoryList.DataBind();
    }
}

أريد أن يكون للفئة الفرعية مسافة بادئة عن طريق إضافة أ <td> علامة على العنصر الذي "ParentId" ليس فارغا. وسؤالي هو كيفية تحرير علامات HTML التي تم إنشاؤها في حدث ItemDatAbound؟

هل كانت مفيدة؟

المحلول

يمكن أن يكون لديك شيء مثل هذا:

<ItemTemplate>
    <tr>
        <td colspan="2"><%# GetParentContent(Eval("ParentID")) %></td>
    </tr>
</ItemTemplate>

في الكود-بيهيند:

protected string GetParentContent(object ParentID)
{
    if(ParentID!=null)
        ... return parent HTML ...
    else
        return "";
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top