Data Manipulation using ListView Server Control with ASP.NET 3.5
page 4 of 10
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 18815/ 567

How to add rows to a ListView Web Server Control

In order to allow users to add rows to the ListView, the following needs to be taken care of:

A Hyperlink or a Button needs to be created for the user to click on. Once the user clicks the link, we should add a blank row to the ListView.

In the insert mode, we should have a “Save” option and a “Cancel” option.

If the user clicks “Save” a new row should be added to the database table

If the user clicks “Cancel” the insert operation should be cancelled.

Let us see how the above can be achieved:

Providing a Hyperlink/Button to add a new row to the Listview

Listing 3

<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
<tr>
      <td colspan="5" align="center">
      <asp:LinkButton Text="Click here to add an Author" ID="lnkNew"
          runat="server" Font-Bold="true" OnClick="NewAuthor"></asp:LinkButton>
      </td>
</tr>
</LayoutTemplate>

In the ListView control definition, you should define a method which will be invoked when the event ItemInserting occurs. For example:

Listing 4

OnItemInserting="InsertList" 

And the InsertList method definition will look like as follows:

Listing 5

protected void InsertList(Object sender, ListViewInsertEventArgs e)
{
    ListView1.InsertItemPosition = InsertItemPosition.None; 
}

ListView has an InsertItemTemplate Template in which we can specify the controls that we need to show during the insert mode. For example:

Listing 6

<InsertItemTemplate>
<tr>
      <td><asp:TextBox id="txtLName" runat="server"></asp:TextBox></td>
      <td>
      <asp:LinkButton ID="lnkSave" CommandName="Save" runat="server" 
      Text="Save"></asp:LinkButton>
 
      <asp:LinkButton ID="lnkCancel" CommandName="Cancel" runat="server" 
      Text="Cancel"></asp:LinkButton>
      </td>
</tr>                
</InsertItemTemplate>

I the above code listing, we have one textbox and two links. The textbox is used to get the data from the user and will eventually be saved to the database. The action link, Save is used to save the data. We should write a custom function which will retrieve the textbox content and inserts the value to the database.  The command name for the Save link should be "Save". The action link, Cancel is used to cancel the insert operation. The command name for the Cancel link should be "Cancel". The code for the ItemCanceling event will look like as follows:

Listing 7

ListView1.InsertItemPosition = InsertItemPosition.None;
((LinkButton)ListView1.FindControl("lnkNew")).Visible = true;

In the above code, the first line removes the newly added row. The second line changes the visible property for "Click here to add an Author" hyperlink which is displayed in the ListView footer.

For saving data to the database, we should write custom code according to our business logic. The main purpose of the Save method should be to get the values for all controls. This can be achieved using the FindControl method. The following code snippet retrieves the value of a textbox control. Once the value is retrieved all we have to do is create a database connection and insert the data to the database.

Listing 8

TextBox txtLname = (TextBox)e.FindControl("txtLName");

Figure 2: ListView Insert mode

 


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 3 and 2 and type the answer here:

User Comments

Title: Nice   
Name: kinng
Date: 9/6/2008 10:38:06 AM
Comment:
Thanks, Useful article.
Title: YOU MUST TAKE THIS ARTICLE DOWN   
Name: HAN
Date: 6/20/2008 11:33:51 AM
Comment:
I spended quite a lot of hours and your codes are not working. I tried to make it work, but it just doesn't make sense and not working. I found it's better to put Updating, Inserting, and deleting on ItemCommand event. If you want to do all actions in code-behind, that's the best place you should embed the action codes. Your codes are too buggy and don't make sense in general. I hope anyone reading this as a beginner should avoid. Don't waste your time thinking you're doing something wrong. It's just the design of this codes are too nonsense
Title: Re: Go back and try again   
Name: Das (Author)
Date: 5/20/2008 9:05:08 PM
Comment:
This is fixed.

Well, this is a design flaw. When we have zero rows, the link to add authors was disabled. This is due to fact that the link to add new authors was in the ItemTemplate. When we have zero rows, the rows inside item template will not get rendered. So, I added an EmptyDataTemplate. Inside the EmptyDataTemplate, I have included a link to add authors. By this way, when someone deletes all rows, the link inside the EmptyDataTemplate will get rendered.
Title: Go back and try again   
Name: BugBuster
Date: 5/20/2008 1:19:20 PM
Comment:
Your code doesn't work, please correct it or remove from site. Tired of finding non-working examples.
Title: Editnig ListView   
Name: ListView
Date: 4/23/2008 3:08:32 AM
Comment:
Unable to edit
Title: Re: Live Demo is broken   
Name: Das
Date: 2/19/2008 6:13:48 PM
Comment:
It is a bug in my code. when the record count goes to zero, the link to "add new records" is not being displayed.

I need to fix this, so that the "Add new" link / button will always show up. so, when we have zero rows, we could add new row using the "Add New" functionality.

Will modify the code accordingly.
Title: Live Demo is broken   
Name: Jason Schechter
Date: 2/19/2008 2:38:06 PM
Comment:
Nothing is coming up except the shell of the table with the column headings.
Title: ListViewItemType   
Name: Wissam Bishouty
Date: 2/19/2008 6:33:06 AM
Comment:
Greetings,
thank you for the useful article.
in the DataBoundList method you say : if (ddl != null)
but why you do not check if the listview in edit mode so if it is in edit mode so you get the reference to the dropdownlist??

Regards.
Title: Re: Could not find stored procedure 'teacher.das_get_authors'.   
Name: Das
Date: 2/17/2008 6:39:19 PM
Comment:
The stored procedure got deleted somehow. Not sure how. But I just ran the create script again and the LIVE demo is working fine again.

Thanks for letting me know about the error
Title: Broken Demo   
Name: me
Date: 2/6/2008 4:43:35 PM
Comment:
Nice....
Could not find stored procedure 'teacher.das_get_authors'.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 9/7/2008 7:21:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search