我关注了Gridview:
<asp:GridView ID="GridView1" runat="server" CssClass="table" DataKeyNames="groupId"
DataSource="<%# dsUserGroupsSelected %>" DataMember="Group" etc....>
并在触发RowDeleting事件处理程序之后:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
e.Keys是空的。而且,在运行时如果我检查
dsUserGroupsSelected.Group.PrimaryKey
它与下面有关:
{System.Data.DataColumn[1]}
[0]: {groupId}
所以这对我来说真的很奇怪。我错过了什么吗?我有这种解决方法:
int groupId = (int)GridView1.DataKeys[e.RowIndex].Value;
这将工作得很好,但我无法理解为什么e.Keys(和e.Values)会是空的!?有什么想法吗?
答案 0 :(得分:4)
看起来这种行为是故意的。
来自http://forums.asp.net/p/1050092/2128091.aspx
在Gridview.HandleDelete()中查看Reflector,看起来如此 e.Keys和e.Values只有在填充时才会填充 gridview.IsBoundUsingDataSourceID。也就是说,如果你设置了DataSource 在代码中,这一切都不会起作用。好的,微软!可能有 有用的提一下,在帮助中也许?? !!拉克伦
编辑:
我最终创建了自己的数据对象并将它们放在app_code文件夹中
离。
public class CustomDataViews
{
public class FileQuery
{
public class File
{
public DateTime CreatedDate { get; set; }
public string FileName { get; set; }
public string Path { get; set; }
public void Delete() { }
}
public ArrayList GetFiles()
{
System.Collections.ArrayList files = new ArrayList();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath("~/UserUpload/"));
foreach (System.IO.FileInfo fi in (from a in di.GetFiles() orderby a.CreationTime descending select a))
{
File myFile = new File();
myFile.CreatedDate = fi.CreationTime;
myFile.FileName = fi.Name;
myFile.Path = "/VACWeb/UserUpload/" + fi.Name;
files.Add(myFile);
}
return files;
}
public void Delete(string FileName)
{
if (FileName != null)
{
string path = HttpContext.Current.Server.MapPath("~/UserUpload/") + FileName;
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
}
}
}
}
ASPX
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" DataKeyNames="FileName"
DataSourceID="ods1">
<Columns>
<cc:ExtendedCommandField DeleteConfirmationText="Are you sure you wish to delete this file?"
DeleteText="Delete" ShowDeleteButton="true" />
<asp:BoundField DataField="CreatedDate" HeaderText="Created Date" DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:ImageField DataImageUrlField="Path" AlternateText="No Image" HeaderText="Image Preview"
ControlStyle-Width="100px">
<ControlStyle Width="100px" />
</asp:ImageField>
<asp:BoundField DataField="Path" HeaderText="Path" />
<asp:HyperLinkField DataNavigateUrlFields="Path" DataTextField="FileName" HeaderText="Link" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ods1" runat="server" DeleteMethod="Delete" SelectMethod="GetFiles"
TypeName="CustomDataViews+FileQuery">
<DeleteParameters>
<asp:Parameter Name="FileName" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>
答案 1 :(得分:1)
简单! ASP是一大块....而且GridView是大平局中的一小部分。有同样的问题,虽然解决方法可以删除,但更新变得非常有趣......实际问题似乎是没有好的或明显的原因如果DataSource不是DataSourceControl,则缺少GridView功能的looooooooot :喜欢填写钥匙和钥匙在eventargs中赋值属性。请享用! Viva Microsoft!
答案 2 :(得分:0)
您是否有可能在Page_Load方法中以编程方式对gridview进行排序?如果是这样,请尝试将排序移动到Page_Init方法,看看是否能解决问题。
答案 3 :(得分:0)
显然,GridViewDeleteEventArgs.Keys属性仅在通过设置 DataSourceID 属性设置GridView的数据源时起作用 - 即设置 DataSource 属性然后手动调用DataBind()会将Keys属性(以及Values属性)保留为空。