Reputation: 13886
Please consider these operations -
Following code displays files in a DataList. There is a delete button beside each row.
<asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand" runat="server">
<ItemTemplate>
<tr>
<asp:Label Text='<%# Eval("ContainingFolder") as string + "\\" + Eval("FileName") as string %>'
Visible="false" ID="lblFullPath" runat="server" />
<td><%# Eval("FileName") %></td>
<td><%# Eval("ContainingFolder") %></td>
<td><%# Eval("FileSize") %></td>
<td><%# Eval("Modified") %></td>
<td>
<asp:LinkButton Text="Delete" ID="linkDelete" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:DataList>
In ItemCommand Event Handler this code deletes the selected file from this list.
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var selectedItem = e.Item.FindControl("lblFullPath") as Label;
e.Item.Visible = false; //doesn't work
File.Delete(selectedItem.Text);
}
However e.Item.Visible = false does not hides the row. As a workaround found here, How to hide an item in datalist
I have wrapped contents inside a placeholder control.
<ItemTemplate>
<asp:PlaceHolder ID="ph1" runat="server">
<%--wrapped content--%>
</asp:PlaceHolder>
</ItemTemplate>
And hiding the placeholder control -
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var selectedItem = e.Item.FindControl("lblFullPath") as Label;
//e.Item.Visible = false;
e.Item.FindControl("ph1").Visible = false;
File.Delete(selectedItem.Text);
}
Generally in asp.net hiding parent control hides all of its child controls.
But I am not able to understand,
Why hiding the parent control e.Item doesn't hide its containing elements, in case of DataList ? Can you please advise.
Thank you for your help.
EDIT: Updating more info about generating list of files to display in DataList.
protected void btnFindDuplicates_Click(object sender, EventArgs e) { DataList1.DataSource = FindDuplicates(); DataList1.DataBind(); }
Upvotes: 2
Views: 2350
Reputation: 21365
I created this sample using JQuery and PageMethods:
<script>
$(function () {
var $list = $("table[id*=myDataListID]");
var $buttons = $("input:submit[id*=remove]", $list);
$buttons.click(function (e) {
e.preventDefault();
if (!confirm("Are you sure?")) {
return false;
}
var $self = $(this);
var $jobID = $self.closest("tr").children().find("span[id*=jobID]");
$buttons.prop("disabled", true);
$.ajax({
url: "<%: this.ResolveClientUrl("~/Topics/JQuery/Ajax/RemoveRowUsingJQueryAfterCallingService.aspx/Remove") %>",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{id: " + $jobID.text() + "}",
async: true,
cache: false,
success: function () {
$buttons.prop("disabled", false);
$self.closest("tr").remove();
},
error: function (XHResponse, errorMessage, errorCode) {
$buttons.prop("disabled", false);
alert(errorMessage);
}
});
});
});
</script>
<asp:DataList runat="server" DataKeyField="job_id" DataSourceID="lds" ID="myDataListID">
<HeaderTemplate>
<tr>
<th>
</th>
<th>
ID
</th>
<th>
Name
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Button Text="Remove" runat="server" ID="remove" />
</td>
<td>
<asp:Label Text='<%# Eval("job_id") %>' runat="server" ID="jobID" />
</td>
<td>
<asp:Label ID="jobDesc" Text='<%# Eval("job_desc") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:DataList>
[WebMethod]
public static void Remove(Int16 id)
{
// simulate deleting the file
Thread.Sleep(3000);
}
I just uploaded the full working example to my GitHub site
Upvotes: 1