Friday, March 27, 2015

Edit,Update,Delete and Insert in ListView Control without code - only configure sql datasource

Edit,Update,Delete and Insert in ListView Control
Introduction
We have explored the basics and adding paging feature to ListView control through my previous articles ListView Control in ASP.Net 3.5 and Paging in ListView in ASP.Net 3.5Our next step in learning ListView Control is to add edit/update/delete/insert feature to the ListView control. Like my previous articles on ListView control, we will add this feature in 2 scenarios.
Ø       Edit/Update/Delete/Insert feature using DataSource controls.
Ø       Edit/Update/Delete/Insert feature without using DataSource controls.
In GridView, the Edit/Update/Delete feature is an inbuilt one and can be achieved without writing a line of code with the help of DataSource controls. Likewise, the ListView control also have inbuilt Edit/Update/Delete feature and additionally, insert feature is also introduced as an inbuilt one.
Edit/Update/Delete/Insert feature using DataSource controls
Configuring SqlDataSource Control
Drag a SqlDataSource control from the data tab of Visual Studio 2008. Configure it with the Database Server; I have used a database in App_Data folder. To configure the DataSource control to handle insert, update and delete click “Advanced...” button. Refer the below snapshot.
Edit/Update/Delete/Insert in ListView in ASP.Net 3.5
Check the “Generate Insert, Update and Delete statements as shown in below figure. Click OK.
F:\Articles\Dot Net 3.5\ListView EditUpdateAndDelete\ConfigDS2.png
Configuring ListView Control
Drag a ListView control from the data tab of Visual Studio 2008.  Click the Smart tag of ListView control and select the datasource control; Refer the below figure, SqlDataSource1 in our example.
F:\Articles\Dot Net 3.5\ListView with DataPager\ConfigLV.png

Click “Configure ListView...” option as seen in the above figure to choose the display Layout for the ListView. Choose a display layout, I have selected “Grid” in our example, as seen in the below figure. Click Ok.
F:\Articles\Dot Net 3.5\ListView EditUpdateAndDelete\ConfigLV.png
You can check “Enable Paging”, which will add a DataPager control in the LayoutTemplate of the ListView control. Read my previous article onPaging in ListView to understand more.
The insert item position can be configured as a LastItem or FirstItem using InsertItemPosition property of ListView control. Executing the page will give you an output similar to the below figure. In the below output, I have added paging feature using DataPager control.
F:\Articles\Dot Net 3.5\ListView EditUpdateAndDelete\LVWithDS.png



if you use dropdown in your insertert template then to passvalue to parameter use below code

Not working:Try manually editing your SqlDataSource1 to make it use your DDL as a parameter with something like:
Fix:

protected void ListView2_OnItemInserting(object sender, EventArgs e)
{
    string sv = ((DropDownList)ListView2.InsertItem.FindControl("DropDownList2")).SelectedValue;
    SqlDataSource31.InsertParameters.Add("Parameteridforinsert", sv);

}

0 comments:

Popular Posts

Recent Posts

Unordered List

Text Widget

Blog Archive