IT Community - Software Programming, Web Development and Technical Support

ListView server control in ASP.Net 3.5

This is a discussion on ListView server control in ASP.Net 3.5 within the Discussweb HQ forums, part of the The Lobby category; Hi all, I am back for a long gape. Here am going to post some useful posts about how to ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > The Lobby > Discussweb HQ

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-26-2008, 02:37 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile ListView server control in ASP.Net 3.5

Hi all,

I am back for a long gape. Here am going to post some useful posts about how to use listview server control in ASP.NET 3.5.

Come lets start!
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-27-2008, 02:02 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: ListView server control in ASP.Net 3.5

Binding a ListView Web Server Control:

Code:
protected void PopulateListView()
{
  SqlConnection myConn = CreateConnection();
  // CreateConnetion is a custom method which returns SQL connection.
  SqlCommand myCmd = new SqlCommand("pubs_get_authors", myConn);
  myCmd.CommandType = CommandType.StoredProcedure;
  try
  {
    myConn.Open();
    ListView1.DataSource = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
  }
  catch (SqlException se)
  {
    lblMsg.Text = se.Message;
  }
  catch (Exception e)
  {
    lblMsg.Text = e.Message;
  }
  finally{}
  ListView1.DataBind();
  myConn.Close();
}
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-27-2008, 02:04 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Talking Re: ListView server control in ASP.Net 3.5

Add Rows to 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.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-27-2008, 02:06 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: ListView server control in ASP.Net 3.5

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


Code:
OnItemInserting="InsertList"
And the InsertList method definition will look like as follows:

Code:
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.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-27-2008, 02:12 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: ListView server control in ASP.Net 3.5

Code:
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.

Code:
TextBox txtLname = (TextBox)e.FindControl("txtLName");
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-28-2008, 03:25 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: ListView server control in ASP.Net 3.5

Add Modify/Edit rows in a ListView Web Server Control

In order for the user to edit a row in the ListView, they should be able to change the corresponding row in the ListView to an editable one.

When the user clicks on any of the Edit Link, the corresponding row in the ListView should be changed to an editable row. This can be achieved using the following code snippet:

Code:
ListView1.EditIndex = e.NewEditIndex;
e is an instance of ListViewEditEventArgs.

Once the ListView is in the edit mode, we have two possibilities. The first being: Saving the edited data back to the database and the second is to cancel the edit operation.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-28-2008, 03:30 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: ListView server control in ASP.Net 3.5

Canceling:

Canceling the edit operation can be done using the following code snippet:

Code:
ListView1.EditIndex = -1;
When the user clicks the Update link after editing the needed data, the event ItemUpdating will be fired. We could invoke a method for this event and within the method the modified data can be retrieved and update the database using a stored procedure or an inline SQL statement. Just like the insert operation, we could use the FindControl method to retrieve the modified values. Another key aspect for any edit operation is that we should know which row in the database should be updated. For this purpose, we could store the primary key for each row in the ListView inside a hidden label control or even a hidden server control.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-29-2008, 12:10 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: ListView server control in ASP.Net 3.5

Editing a DropDownList control within the edit mode of ListView

Editing a textbox control is very simple. But if you have a dropdown listbox, then pre-setting the old value for the dropdown might need some extra coding. There could be many ways you can pre-set the old value for the DropDownList control within the edit mode. One of the ways is as follows:

When the user clicks the Edit link for any row, get the value for the DropDownList control and store it in a global variable.

The event, ItemEditing will be fired when the edit link is clicked. In the ItemTemplate template for the ListView, the value for the DropDownList control is displayed as a label.

All we have to do is get the value of the label server control.

Using this label, we could change the index for the DropDownList control. The following code retrieves the old value for the DropDownList control (which is stored in a Label control)

Code:
ListView1.EditIndex = e.NewEditIndex;
// Get the Value for State
Label lblTemp = (Label)ListView1.Items[e.NewEditIndex].FindControl("lblState");
strCurrentState = lblTemp.Text;
Now using the string, strCurrentState we could change the index of the DropDownList control within the ItemDataBound event.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-29-2008, 12:12 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: ListView server control in ASP.Net 3.5

The following method will be invoked during an ItemDataBound event occurs.


Code:
protected void DataBoundList(Object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    // Get a handle to the ddlState DropDownList control
    DropDownList ddl = (DropDownList)e.Item.FindControl("ddlState");
     // Make sure we have the handle !
    if (ddl != null)
    {
      ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText
        (strCurrentState));
    }
  }
}
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-31-2008, 03:07 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: ListView server control in ASP.Net 3.5

Delete a row from a ListView Web Server Control

The first step to allow users to delete a row from the ListView is to provide them with a Delete Link / Delete button for every row in the ListView.

When the Delete link is clicked, the ItemDeleting event will be fired. We could invoke a method for this event. Within the method we could get the value for the primary key and invoke a stored procedure or use an inline SQL Delete statement.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 03-31-2008, 03:07 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: ListView server control in ASP.Net 3.5

Display a warning message when the user clicks Delete in a ListView Control


It will be always helpful to ask user before they actually delete the row from the ListView and eventually from the database. We could display a warning message whether they really intend to delete the row. If the reply is either yes or OK, then we could carry out with the actual removal of the rows from the database.

This can be done using the following technique:

During the ItemDataBound event, we can attach a client side event for each Delete Link in the ListView control.

Code:
protected void DataBoundList(Object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
    if (lnkDelete != null)
    {
      lnkDelete.Attributes.Add("onclick",
        "return confirm('Are you sure you want to delete this author?');");
    }
  }
}
Clicking on the delete link will result in the following alert box. If the user clicks cancel, then the delete operation will not be carried out.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to control the webcam using C#? kingmaker C# Programming 2 04-07-2008 01:20 AM
Showing the Image file thumbnail view in ListView control using VC++ 6.0 prabhat.singh C and C++ Programming 0 01-17-2008 09:53 PM
publicdomainregistry - control panel? annerz General Web hosting Discussions 0 11-06-2007 08:10 PM
How do I get the value of a form control? itbarota HTML, CSS and Javascript Coding Techniques 2 10-26-2007 03:13 AM
use of Custom Control and User Control? a.deeban ASP and ASP.NET Programming 1 08-20-2007 07:25 AM


All times are GMT -7. The time now is 10:03 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0