none
"#include file" doesn't work in a repeater ? RRS feed

  • Question

  •  

    Hi,

     

    I try to display the content of 2 aspx pages in one page.  These 2 pages are stored into a  directory (form) in my visual studio project. For all this pages, there is a row with their path  in a sql table.

     

    my sql table :

     

    id                    htmlpath

    1                     form/page1.aspx

     

    2                     form/page2.aspx

     

    In C# I set a Sqldatareader and bind it on my repeater.

     

     

    my code :

    <asp:Repeater ID="Repeater1" runat="server">

    <HeaderTemplate>

    <asp:Label ID="Label1" runat="server" Text="title"></asp:Label>

    </HeaderTemplate>

     

    <ItemTemplate>

    <div align="center" >

    <!--#include file=<%#DataBinder.Eval(Container.DataItem,"htmlpath")%>-->

    </div>

    </ItemTemplate>

    </asp:Repeater>

     

    It doesn't work but the source code seems fine :

     

    Source code :

    <span id="Repeater1_ctl00_Label1">Titre</span>
            
                 <div align="center" > 
                    <!--#include file=form/form01.aspx-->
        
                 </div>
           
                 <div align="center" > 
                    <!--#include file="form/form02.aspx"-->
        
                 </div>

     

     

    I m wondering if there is no way to work with repeater and include.

     

    If it's true, how can I display automatically my aspx pages which path are stored in a sqldatabase ?

     

    Thx

    Friday, June 13, 2008 8:45 AM

Answers

  • Yes, "include" is not "recommended" in ASP.NET, we use user controls instead.

     

    For using user controls, we could use them as follows:

     

    <asp:Repeater ID="Repeater1" runat="server">

    <HeaderTemplate>

    <asp:Label ID="Label1" runat="server" Text="title"></asp:Label>

    </HeaderTemplate>

     

    <ItemTemplate>

    <div align="center" >

    <uc1:UserControl1 ID="uc1" runat="server" />

    <uc2:UserControl2 ID="uc2" runat="server" />

    <uc3:UserControl3 ID="uc3" runat="server" />

    <uc4:UserControl4 ID="uc4" runat="server" />

    ......

    </div>

    </ItemTemplate>

    </asp:Repeater>

     

     

    And you could write code in ItemDataBound event handler of the repeater

     

    protected void Repeater1_ItemDataBound(blablabla bla, blabla, blaa)

    {

    htmlpath = (Repeater1.DataSource as DataTable).Rows[e.ItemIndex]["htmlpath"] as string;

     

    Control uc1 = e.Item.FindControl("uc1");

    Control uc2 = .....

    ....

    ....

     

    uc1.Visible = false;

    uc2......

    ....

    ....

     

    switch(htmlpath)

    {

    case "path1":

    uc1.Visible = true;

    break;

    case "path2":

    .....

    ....

    ....

    }

    }

     

    If the control is not visible, it would not be rendered. So it would not take too much cost to have every user controls in the item template.

     

     

    Tuesday, June 17, 2008 7:01 AM

All replies

  • You couldn't do it like this. If you do need this, you could implement it as follows:

     

    <asp:Repeater ID="Repeater1" runat="server">

    <HeaderTemplate>

    <asp:Label ID="Label1" runat="server" Text="title"></asp:Label>

    </HeaderTemplate>

     

    <ItemTemplate>

    <div align="center" >

    <%# OpenFileContent(DataBinder.Eval(Container.DataItem,"htmlpath") as string)%>

    </div>

    </ItemTemplate>

    </asp:Repeater>

     

     

    and you have a protected method OpenFileContent to open the content of corresponding file. And you could use StreamReader to open files and get the contents.

     

    For more information about opening a file and get its content, you could visit http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

    Monday, June 16, 2008 10:56 AM
  • Hi,

     

    Thanks it 's helpfull but I'v still having a problem :

     

    My include files are aspx files .in source code I see somme asp objects which are of course not displayed .

     

    Moreover I've read that Asp.net does not process server-side script in the dynamically included file. It'll be soon a problem for my work.

     

    I m wondering how dynamically include aspx file into another.

     

    Maybe I'v chosen a wrong way from start...

     

     

    I think I should look to user control side.

    Monday, June 16, 2008 1:21 PM
  • Yes, "include" is not "recommended" in ASP.NET, we use user controls instead.

     

    For using user controls, we could use them as follows:

     

    <asp:Repeater ID="Repeater1" runat="server">

    <HeaderTemplate>

    <asp:Label ID="Label1" runat="server" Text="title"></asp:Label>

    </HeaderTemplate>

     

    <ItemTemplate>

    <div align="center" >

    <uc1:UserControl1 ID="uc1" runat="server" />

    <uc2:UserControl2 ID="uc2" runat="server" />

    <uc3:UserControl3 ID="uc3" runat="server" />

    <uc4:UserControl4 ID="uc4" runat="server" />

    ......

    </div>

    </ItemTemplate>

    </asp:Repeater>

     

     

    And you could write code in ItemDataBound event handler of the repeater

     

    protected void Repeater1_ItemDataBound(blablabla bla, blabla, blaa)

    {

    htmlpath = (Repeater1.DataSource as DataTable).Rows[e.ItemIndex]["htmlpath"] as string;

     

    Control uc1 = e.Item.FindControl("uc1");

    Control uc2 = .....

    ....

    ....

     

    uc1.Visible = false;

    uc2......

    ....

    ....

     

    switch(htmlpath)

    {

    case "path1":

    uc1.Visible = true;

    break;

    case "path2":

    .....

    ....

    ....

    }

    }

     

    If the control is not visible, it would not be rendered. So it would not take too much cost to have every user controls in the item template.

     

     

    Tuesday, June 17, 2008 7:01 AM