ASP.net - How to populate DataBinder from textfile

Discussion in 'Programming' started by alfred dj, Jan 14, 2014.

  1. #1
    I have been searching online for the best way to populate databinder within a repeater. I have no luck in finding the best solution. The closest I got to was using datagrid that you can populate with a resultset from database.

    The data that I would like to display is from a csv file. It has 5 items in each row. Each of these items represent a field in the webpage. I have no problem getting the value from csv file.

    How do I populate strSource with the value from csv file? Do I use array, arraylist or anything?

    in .aspx.vb:
    RepeaterImages.DataSource = strSource

    in .aspx
    <asp:Repeater ID="RepeaterImages" runat="server">
    <ItemTemplate>
    <asp:Textbox ID="txtTextbox1" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "item1")%>' </asp:Textbox>
    <asp:Textbox ID="txtTextbox2" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "item2")%>' </asp:Textbox>
    <asp:Textbox ID="txtTextbox3" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "item3")%>' </asp:Textbox>
    <asp:Textbox ID="txtTextbox4" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "item4")%>' </asp:Textbox>
    <asp:Textbox ID="txtTextbox5" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "item5")%>' </asp:Textbox>
    </ItemTemplate>
    </asp:Repeater>
     
    alfred dj, Jan 14, 2014 IP
  2. Dom Fraser

    Dom Fraser Greenhorn

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #2
    You need to do 2 things:

    1. Set the DataSource property. This must be an object that implements the IEnumerable or IListSource interface, so it can be a Generic List, ArrayList or similar. If you can get your csv data into an ArrayList, then that should be OK.

    2. Call the DataBind method for the Repeater to bind the control to the DataSource.

    So your code would look like:

    RepeaterImages.DataSource = myDataSource
    RepeaterImages.DataBind()
     
    Dom Fraser, Jan 14, 2014 IP
  3. alfred dj

    alfred dj Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3


    Can you show me a code how to populate the myDataSource? that is what i'm having trouble with.
     
    alfred dj, Jan 14, 2014 IP
  4. Dom Fraser

    Dom Fraser Greenhorn

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #4
    Dom Fraser, Jan 14, 2014 IP