Reputation: 567
Hello and Good Morning,
I've been working on a small project to store some data in a MySQL database. So far I've been able to follow online tutorials and get help with my code from friends and others on this forum. But now, I want to do something that I can't seem to find any tutorials for. Hence my coming here.
I have data in a MySQL database and I know how to use a SELECT statement to retrieve the records and assign to a DataTable, then populate the ListView control with the contents of the DataTable. Now I'd like help on how to delete those records (individually and collectively by use of buttons). I know the ListView control has a property that allows Checkboxes on each row but I don't know how to use that to select the row(s) I want deleted. Also, I did some reading and found that I can use the .Remove() method to do this, but how do I pass that back to the database?
If anybody knows a tutorial that can help me, please point me in the right direction. Alternatively, if you'll be kind enough to assist me here as well, I'd be grateful.
//Kismet
Upvotes: 1
Views: 901
Reputation: 274
you can use Listview's onitemcommand Event.
Provide your database primary key as dataKeyNames in your Listview.
<asp:ListView ID="ListView1" runat="server" DataSourceID="AccessDataSource1"
GroupItemCount="3" ItemPlaceholderID="itemPlaceholder" DataKeyNames="Id">
Take a linkbutton as your delete button.
<asp:LinkButton ID="lnkDeletebtn" runat="server" ForeColor="Blue" Font-Underline="true" CommandName="Delete" >Delete</asp:LinkButton>
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.ToString()=="Delete")
{
//here get your datakey
string id = listview1.DataKeys[e.ItemIndex].Value.ToString();
//Now you can fire your delete query
}
}
Upvotes: 2