0

I have an EmptyDataTemplate in my asp:ListView which I want to use to insert a new record.

I have Inserting working in the InsertItemTemplate... I thought I could copy the InsertItemTemplate into the EmptyDataTemplate, on clicking Insert this gives the error

Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert.

How can I use the EmptyDataTemplate to Insert a row? Do I need to use the OnClick of the button to access the values within the EmptyDataTemplate and do an Insert by myself?

I'm using LinqDataSource

Paul Rowland
  • 8,244
  • 12
  • 55
  • 76

3 Answers3

2

You might have figured it by now but if you set the InsertItemPosition to anything other than None the EmptyData Template will not be rendered i.e it will always show the insert template

you can read more here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.emptydatatemplate.aspx

Pankaj Kumar
  • 1,748
  • 6
  • 28
  • 41
  • This solved my issue: How can i insert items in my listview if I'm just starting out and my data set is empty? - use InsertItemPosition="LastItem". – russds Apr 16 '13 at 16:20
1

No way if want to insert data in empty data template.

kingofswing
  • 26
  • 1
  • 4
0

It is possible to do an insert from the EmptyDataTemplate by handcrafting the insert. I am using a listview to display a static number of rows based on a unique filtered item. I am basically listing all the static attributes of an object. In the case where a new object is filtered on that does not have any attributes associated with it, i use the EmptyDataTemplate of the listview to display a HTMLTable that contains asp.net controls to capture data. I have a command button within the table that i evaluate using the ListView_ItemCommand. If the CommandName matches that of the "Insert" button within the EmptyDataItem, I use the ListView.Controls(0).FindControl method to locate my table. I then loop through my table and do inserts on the data found within each row. I included the how to find a control within the htmltable. In my code I am actually grabbing a bunch of controls then crafting the sql and using a SQLConnection to perform the insert.

Protected Sub ListView_ItemCommand(sender As Object, e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView.ItemCommand

  Select Case e.CommandName
    Case "Submit"
      Dim edt As HtmlTable = ListView.Controls(0).FindControl("myhtmltable")
      Dim ddl As DropDownList = CType(edt.FindControl("mydropdownlist"), DropDownList)
      'Perform Insert
     Case "Some other commandname"
  End Select
End Sub

You will need to still do error checking and databind() and refresh your listview.

Is this the best way. Maybe not... But it is possible.

~Ian

Ian B
  • 115
  • 2
  • 8