Delete rows in DataList using ItemCommand event


In this article we are going to see how we can delete items in a DataList using ItemCommand event of DataList. Here all the rows for which Delete option is checked will be deleted. We use button in a Footertemplate to fire the ItemCommand event.

In this article we are going to see how we can delete items in a DataList using ItemCommand event of DataList. Here all the rows for which Delete option is checked will be deleted.

Step1: Drag and Drop a DataList from the toolbox and add the HeaderTemplate,ItemTemplate and FooterTemplate which displays the Delete button as shown below.


<asp:DataList ID="DataList1" runat="server" DataKeyField="PKCourseId" DataSourceID="SqlDataSource1"
RepeatLayout="Flow" onitemcommand="DataList1_ItemCommand">
<HeaderTemplate>
<table width="30%" style="background-color:Gray;border:1px;">
<tr>
<td style="width:33%">
CourseId
</td>
<td style="width:33%">
CourseName
</td>
<td style="width:33%">
Delete
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="30%">
<tr>
<td style="width:33%">
<asp:Label ID="lblCourseId" runat="server" Text='<%# Eval("PKCourseId") %>' />
</td>
<td style="width:33%">
<asp:Label ID="CourseNameLabel" runat="server" Text='<%# Eval("CourseName") %>' />
</td>
<td style="width:33%">
<asp:CheckBox runat="server" ID="chkDelete" />
</td>
<td>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table width="25%">
<tr>
<td align="right">
<asp:Button runat="server" ID="btnDelete" Text="Delete" CommandName="Delete" />
</td>
</tr>
</table>
</FooterTemplate> </asp:DataList>


In the above DataList(DataList1), DataKeyField is the Primary key column name. DataSourceId is the datasource which displays the data in DataList. RepeatLayout can be specified as Flow or vertical, but I have customized the DataList as per my requirement to display in a table.
Step2: Drag and Drop a SqlDataSource and customize it to get the data from the required table. Here we are getting the PKCourseId,CourseName columns from the Course table.

SelectCommand="SELECT [PKCourseId], [CourseName] FROM [Course]">


Step3:
Go to Properties(F4) of DataList1 and then go to events tab as shown below and double click on ItemCommand event to generate the event.
ItemCommand event

In this event Write the below code to get all the PKCourseId of the rows checked to delete on ItemCommand event of DataList. If the type of the row is either Item or AlternatingItem then Get the reference to the checkbox(chkDelete), If the checkbox is checked then get the pkcourse from the label in the same row and store it in the string and seperate each courseId using ",". Using Trim function we are removing the extra "," from the string. Now we are passing this comma separated strin of ids to the DeleteData method to delete the selected courses from the database then bind the Datalist with the data.

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
string strCourseId = string.Empty;
//Iterate through each row of the datalist.
foreach (DataListItem item in DataList1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
bool isChecked = Convert.ToBoolean((((CheckBox)(item.FindControl("chkDelete"))).Checked));
if (isChecked)
{
int courseId = Convert.ToInt32((((Label)(item.FindControl("lblCourseId"))).Text));
strCourseId += courseId;
strCourseId += ",";
}
}
}
strCourseId = strCourseId.Trim(',');
int rowsDeletedCount = DeleteData(strCourseId);
DataList1.DataBind();
}

protected int DeleteData(string courseId)
{
//Connect to database and execute the delete command , In the where clause
//of the delete command there is a comma seperated list of courseIds of the courses which has to be deleted.
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=OnlineTestDB;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("delete from course where PKCourseId in (" + courseId +")", con);
int rowsDeleted = cmd.ExecuteNonQuery();
return rowsDeleted;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: