Bind formview to server side datatable

Posted on 2012-08-14
Last Modified: 2012-08-15
Dear exports,

I use a formview to display some details of a datagrid. The data is bind to labels
<asp:Label ID="Label2" runat="server"><%# Eval("ReportNumber")%></asp:Label> with a datasource defined client side.

The data is bind 1 on 1, but I need to make changes to some data before it's being bind / displayed. The filed usernames can for example contain multiple comma separated usernames of which the top 3 should be displayed under each other.

Is it possible to create a datatable server side (instead of client side) and bind this to the labels in the formview, this way I am able to create the datatable the way I need it?

Thanks in advance,
Question by:mark_norge
    LVL 83

    Expert Comment

    You can change your SQL query to only get first three users.

    Author Comment

    Thanks CodeCruiser,

    This is possible, but it's a bit more complicated. The string has the following structure username1#date1¤,username2#date2¤,username3#date3¤, etc. I have the following function which I use to split a comma separated string. Then I would need to adjust this to get out username (and preferably also date).


        declare @idx int      
        declare @slice varchar(8000)    

        select @idx = 1      
            if len(@String)<1 or @String is null  return      

        while @idx!= 0      
            set @idx = charindex(@Delimiter,@String)      
            if @idx!=0      
                set @slice = left(@String,@idx - 1)      
                set @slice = @String      

                insert into @temptable(Items) values(@slice)      

            set @String = right(@String,len(@String) - @idx)      
            if len(@String) = 0 break      
    LVL 20

    Expert Comment

    I'm not sure if I understood your question properly, but this is how you can create a DataTable server side.

    Server Side DataTable
    LVL 83

    Expert Comment

    Another option is to not use a SQLDataSource and use a datatable. Then you can loop through the datatable, do any processing you want, and then bind it to formview.

    Author Comment

    Thanks again for the replies,

    Creating the datatable would be my preferred solution. I however have difficulties binding the datatable to the formview.

    In the ascx I use (simplified, but I can confirm this is working with a client side datasource)
                <asp:formview id="ReportsView" runat="server" datakeynames="ReportID">
                       <span class='title'>Voyage</span>
                        <asp:Label CssClass="info" ID="Label1" runat="server"><%# Eval("Voyage")%></asp:Label>

    In the code behind I have

    Public Class AlarmReportsDetails
        Inherits System.Web.UI.UserControl

        Public Property ReportID() As String
                If ViewState("ReportID") Is Nothing Then
                    Return ""
                End If
                Return DirectCast(ViewState("ReportID"), String)
            End Get
            Set(ByVal value As String)

                ViewState("ReportID") = value
            End Set
        End Property

        Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
            Me.AlarmReportsDataSource.SelectParameters("ReportID").DefaultValue = Me.ReportID

        End Sub

    End Class

    I created a datatable (which was correctly filled) and try to bind it to the formview using me.formview.datasource=datatable. This however did not work.

    Do you perhaps have an example where a datatable is bind to a formview and where I can see how the label or textbox values are bind client side (eval)?

    Thanks in advance,
    LVL 83

    Accepted Solution

    As long as all the column names are same, all you need to do is fill the datatable and assign it as datasource to the formview. Do add the FormView1.DataBind() as well after assigning the source and do this in page load within a

    If Not Page.IsPostBack


    Author Closing Comment

    Thanks for the help,

    If placed in the load event and not in a Page.IsPostBack it works.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    VB.Net - 2005 Project Not Responding 24 41
    HTML table 7 38 produce a pdf from a GridView 2 26
    popup message 3 17
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now