问题
Ok my first post and i hope the title makes sense.
I have an updatepanel, and inside it resides a fileupload control with a button to trigger an upload. Beneath that i have a ListView which is databinded in the behind file with a list of files that is uploaded. The updatepanel has a "PostBackTrigger" pointed to the upload button.
All of this works just as it should. For each item that is listed, there is a linkbutton which deletes that specific file. That also works as it should BUT here is the thing: It does not trigger a postback and i tried numerous methods after searching the web and not to mention stackoverflow for answers. I tried a lot but nothing really happens even if what looks like the best solution is implemented.
The ascx file (yes its a usercontrol if that matters):
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="LabelUploadFile" runat="server" Text="Upload fil:"></asp:Label>
<br />
<asp:FileUpload ID="FileUploadDocument" runat="server" />
<br />
<asp:DropDownList ID="DropDownListDocumentType" runat="server"></asp:DropDownList>
<br />
<asp:Button ID="ButtonUploadFile" runat="server" Text="Upload fil" CssClass="nice small radius action button" onclick="ButtonUploadFile_Click" />
<br />
<br />
<br />
<asp:ListView ID="ListViewDocuments" runat="server" OnItemCommand="ListViewDocuments_ItemCommand">
<LayoutTemplate>
<table border="0" cellpadding="1">
<tr>
<th align="left">Type</th>
<th align="left">Dokument</th>
<th></th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="lblName"><%#Eval("Type") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblType"><%#Eval("Dokument") %></asp:Label></td>
<td><asp:LinkButton ID="DeleteButton" OnClientClick="return confirm('Slet dokument?');" CommandName="Delete" CommandArgument='<%# Eval("id")%>' runat="server" Text="Slet"></asp:LinkButton></td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<tr>
<td> </td>
<td>Du har ikke uploadet filer endnu.</td>
<td> </td>
</tr>
</EmptyDataTemplate>
</asp:ListView>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ButtonUploadFile" />
<asp:AsyncPostBackTrigger ControlID="ListViewDocuments" EventName="ItemCommand" />
</Triggers>
(Note the asyncpostbacktrigger is just another solution i tried out which i have not removed. Also a scriptmanager is present, it is just not represented in the above code)
The ListViewDocuments_ItemCommand from the behind file:
protected void ListViewDocuments_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
//Send the file's ID to the data layer for deletion
_talentDataAccess.DeleteTalentFileByFileId(Convert.ToInt32(e.CommandArgument));
//Rebind the listveiw with a new list of files.
_fillFileList();
}
}
So like i said, technically everything works, but in short the linkbutton does not refresh the updatepanel.
If there is any questions or need for other code snippets, i will respondt promptly.
Thank you in advance.
回答1:
It's possible that the CommandName="Delete"
is raising the events ItemDeleted
and ItemDeleting
and not ItemCommand
. Although I have to say if this was the case then I would expect the page to crash due to the absence of those events (see MSDN for more details).
That said when I have used the Delete (or Edit) word as a command I've found problems. So I would try the following
- Change the name of the command to something like
CommandName="ItemDelete"
. Does theListViewDocuments_ItemCommand
fire now - Also - to see if it is the update panel causing problems I would temporarily remove it until you are convinced that the item command fires as you want.
Hope it helps some
来源:https://stackoverflow.com/questions/11327047/asp-net-triggering-asyncpostback-with-linkbutton-inside-listview-which-is-insi