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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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! |
| Sponsored Links |
| |||
| 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 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! |
| |||
| In the ListView control definition, you should define a method which will be invoked when the event ItemInserting occurs. For example: Code: OnItemInserting="InsertList" Code: protected void InsertList(Object sender, ListViewInsertEventArgs e)
{
ListView1.InsertItemPosition = InsertItemPosition.None;
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Code: ListView1.InsertItemPosition = InsertItemPosition.None;
((LinkButton)ListView1.FindControl("lnkNew")).Visible = true; 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 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; 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! |
| |||
| Canceling: Canceling the edit operation can be done using the following code snippet: Code: ListView1.EditIndex = -1;
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 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;
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 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! |
| |||
| 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! |
| |||
| 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?');");
}
}
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |