ASP.NET GridView GridViewDeleteEventArgs.Keys empty

I have the following Gridview:

<asp:GridView ID="GridView1" runat="server" CssClass="table" DataKeyNames="groupId"
          DataSource="<%# dsUserGroupsSelected %>" DataMember="Group" etc....>

      

and after running the RowDeleting event handler:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

      

e.Keys is empty. Moreover, at runtime, if I check

dsUserGroupsSelected.Group.PrimaryKey

      

it is called with:

{System.Data.DataColumn[1]}
    [0]: {groupId}

      

so this is really strange to me. Am I missing something? I have a workaround like this:

int groupId = (int)GridView1.DataKeys[e.RowIndex].Value;

      

which will work fine, but I just can't figure out why e.Keys (and e.Values) would be empty !? Any ideas?

+2


source to share


4 answers


It looks like this behavior is intentional.

From http://forums.asp.net/p/1050092/2128091.aspx

Looking at the reflector at the Gridview.HandleDelete () it seems that e.Keys and e.Values ​​only get populated if gridview.IsBoundUsingDataSourceID. That is, if you set the DataSource in code, then none of this will work. Good Microsoft! It should have been helpful to mention that it is possible to help ?? !! Lachlan

Edit:

I ended up creating my own data objects and put them in the app_code folder



ex.

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>

      

+4


source


Simple! ASP is a big chunk .... and GridView is a small chunk in the big world. You have the same problem, and while the workaround is fine for deletion, the update becomes really interesting ... The actual problem is that for some good or obvious reason the looooooooot functionality of the GridView is missing unless the DataSource is a DataSourceControl: How to populate key properties and values ​​in eventargs. Enjoy! Viva Microsoft!



+1


source


Is it possible that you are sorting the gridview programmatically in the Page_Load method? If so, try moving the sort to the Page_Init method and see if that fixes the problem.

0


source


Apparently the GridViewDeleteEventArgs.Keys property only works if you have set the DataSource GridView by setting the DataSourceID property , that is, setting the DataSource property and then manually calling DataBind () leaves the Keys property (and the Values ​​property as well) blank.

Source: http://forums.asp.net/t/1050092.aspx

0


source







All Articles