views:

88

answers:

3

It is known that the ListView control can't display both an EmptyDataTemplate and a InsertItemTemplate at the same time.

For my design style I need to be able to show both. I want to be able to show that no data exist and at the same time show a form to add new data.

I've already implemented various solutions, such as putting a PlaceHolder in my LayoutTemplate and then manually showing or hiding this PlaceHolder in the code-behind, depending on if there is data or not.

However, I would like a control that has this built-in capability in order to keep my code-behind light.

I believe there are only two ways to achieve what I want:

  1. First way (preferred) is to write that custom control myself. I was thinking of deriving from ListView and overriding the function responsible for disabling the EmptyDataTemplate, but I have no experience with custom controls. And I'm not even sure it will work in the end.

  2. Second way is to use a custom control found or purchased somewhere. I have not been able to find such control that has the same base capabilities as the ListView.

Has anybody any idea how to solve #1 and maybe #2?

Thank you.

A: 

I don't understand much of your requirement without a screen shot of what you are actually trying to achieve. Anyway, you may be able to achieve this interface with a combination of ListView+FormView or ListView+ a User Control. If you can provide any more info I may help further.

Popo
+1  A: 

I would go for your option 1: Create a custom control Because you haven't specified a programming language I made one in VB.NET:

Public Class CustomListView
    Inherits ListView
    Public Sub CheckEmptyData() Handles Me.PreRender
        If Me.Items.Count = 0 Then
            Dim label As New Label
            label.Text = "No data found <br/>"
            Me.Controls.AddAt(0, label)
        End If
    End Sub
End Class

Just tested it and works perfectly, it can just replace an existing ListView.

As you can see it checks if there is any data and if not it inserts a label with the text "No data found". I haven't found an easy way to use the EmptyDataTemplate for this, that would be a better option but this might already work for you.

Another option is to hide the InsertItem (InsertItemPosition.None) if there is no data, and add a Button "Insert" to the EmptyDataTemplate that enables the InsertItemTemplate and therefore hides the EmptyDataTemplate.

Willem
Thanks a lot, that helped me solve my issue. I started from your suggestion and I worked my way up to a perfect resolution. I will award you the points because you deserve it, but I will add my own solution.
md1337
Not that I'm expecting an answer after I gave out all the points but... There is an issue with this. In the InsertItemTemplate I have a RequiredFieldValidator on the field used to insert new items, and it keeps getting triggered...
md1337
You mean it keeps getting triggered and fails to validate even if the TextBox(?) has a value? Or does it get triggered when you try to do something else on the page (other button or something) and are not trying to insert an item? If the second is the case try adding a validationgroup to the validator, the textbox and the insert-button.
Willem
Your first guess, it keeps getting triggered and I have a value in the TextBox. Looks like adding an item to the Controls collection somehow messes up the expectation of the validator.
md1337
Seems like the validator does not target the right control anymore, but I have no clue why...
Willem
I solved it, see above.
md1337
+3  A: 

Here is what I ended up doing:

public class MyListView : ListView
{
    protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
    {
        int itemCount = base.CreateChildControls(dataSource, dataBinding);

        if (this.InsertItemPosition != InsertItemPosition.None && itemCount == 0)
        {
            CreateEmptyDataItem();
        }

        return itemCount;
    }
}

Works great!

md1337
Great solution, I must have overlooked the CreateEmptyDataItem function when I wrote my code. Thanks for the bounty!
Willem